You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook_test.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package logrus_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "sync"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. . "github.com/sirupsen/logrus"
  10. . "github.com/sirupsen/logrus/internal/testutils"
  11. )
  12. type TestHook struct {
  13. Fired bool
  14. }
  15. func (hook *TestHook) Fire(entry *Entry) error {
  16. hook.Fired = true
  17. return nil
  18. }
  19. func (hook *TestHook) Levels() []Level {
  20. return []Level{
  21. TraceLevel,
  22. DebugLevel,
  23. InfoLevel,
  24. WarnLevel,
  25. ErrorLevel,
  26. FatalLevel,
  27. PanicLevel,
  28. }
  29. }
  30. func TestHookFires(t *testing.T) {
  31. hook := new(TestHook)
  32. LogAndAssertJSON(t, func(log *Logger) {
  33. log.Hooks.Add(hook)
  34. assert.Equal(t, hook.Fired, false)
  35. log.Print("test")
  36. }, func(fields Fields) {
  37. assert.Equal(t, hook.Fired, true)
  38. })
  39. }
  40. type ModifyHook struct {
  41. }
  42. func (hook *ModifyHook) Fire(entry *Entry) error {
  43. entry.Data["wow"] = "whale"
  44. return nil
  45. }
  46. func (hook *ModifyHook) Levels() []Level {
  47. return []Level{
  48. TraceLevel,
  49. DebugLevel,
  50. InfoLevel,
  51. WarnLevel,
  52. ErrorLevel,
  53. FatalLevel,
  54. PanicLevel,
  55. }
  56. }
  57. func TestHookCanModifyEntry(t *testing.T) {
  58. hook := new(ModifyHook)
  59. LogAndAssertJSON(t, func(log *Logger) {
  60. log.Hooks.Add(hook)
  61. log.WithField("wow", "elephant").Print("test")
  62. }, func(fields Fields) {
  63. assert.Equal(t, fields["wow"], "whale")
  64. })
  65. }
  66. func TestCanFireMultipleHooks(t *testing.T) {
  67. hook1 := new(ModifyHook)
  68. hook2 := new(TestHook)
  69. LogAndAssertJSON(t, func(log *Logger) {
  70. log.Hooks.Add(hook1)
  71. log.Hooks.Add(hook2)
  72. log.WithField("wow", "elephant").Print("test")
  73. }, func(fields Fields) {
  74. assert.Equal(t, fields["wow"], "whale")
  75. assert.Equal(t, hook2.Fired, true)
  76. })
  77. }
  78. type SingleLevelModifyHook struct {
  79. ModifyHook
  80. }
  81. func (h *SingleLevelModifyHook) Levels() []Level {
  82. return []Level{InfoLevel}
  83. }
  84. func TestHookEntryIsPristine(t *testing.T) {
  85. l := New()
  86. b := &bytes.Buffer{}
  87. l.Formatter = &JSONFormatter{}
  88. l.Out = b
  89. l.AddHook(&SingleLevelModifyHook{})
  90. l.Error("error message")
  91. data := map[string]string{}
  92. err := json.Unmarshal(b.Bytes(), &data)
  93. require.NoError(t, err)
  94. _, ok := data["wow"]
  95. require.False(t, ok)
  96. b.Reset()
  97. l.Info("error message")
  98. data = map[string]string{}
  99. err = json.Unmarshal(b.Bytes(), &data)
  100. require.NoError(t, err)
  101. _, ok = data["wow"]
  102. require.True(t, ok)
  103. b.Reset()
  104. l.Error("error message")
  105. data = map[string]string{}
  106. err = json.Unmarshal(b.Bytes(), &data)
  107. require.NoError(t, err)
  108. _, ok = data["wow"]
  109. require.False(t, ok)
  110. b.Reset()
  111. }
  112. type ErrorHook struct {
  113. Fired bool
  114. }
  115. func (hook *ErrorHook) Fire(entry *Entry) error {
  116. hook.Fired = true
  117. return nil
  118. }
  119. func (hook *ErrorHook) Levels() []Level {
  120. return []Level{
  121. ErrorLevel,
  122. }
  123. }
  124. func TestErrorHookShouldntFireOnInfo(t *testing.T) {
  125. hook := new(ErrorHook)
  126. LogAndAssertJSON(t, func(log *Logger) {
  127. log.Hooks.Add(hook)
  128. log.Info("test")
  129. }, func(fields Fields) {
  130. assert.Equal(t, hook.Fired, false)
  131. })
  132. }
  133. func TestErrorHookShouldFireOnError(t *testing.T) {
  134. hook := new(ErrorHook)
  135. LogAndAssertJSON(t, func(log *Logger) {
  136. log.Hooks.Add(hook)
  137. log.Error("test")
  138. }, func(fields Fields) {
  139. assert.Equal(t, hook.Fired, true)
  140. })
  141. }
  142. func TestAddHookRace(t *testing.T) {
  143. var wg sync.WaitGroup
  144. wg.Add(2)
  145. hook := new(ErrorHook)
  146. LogAndAssertJSON(t, func(log *Logger) {
  147. go func() {
  148. defer wg.Done()
  149. log.AddHook(hook)
  150. }()
  151. go func() {
  152. defer wg.Done()
  153. log.Error("test")
  154. }()
  155. wg.Wait()
  156. }, func(fields Fields) {
  157. // the line may have been logged
  158. // before the hook was added, so we can't
  159. // actually assert on the hook
  160. })
  161. }
  162. type HookCallFunc struct {
  163. F func()
  164. }
  165. func (h *HookCallFunc) Levels() []Level {
  166. return AllLevels
  167. }
  168. func (h *HookCallFunc) Fire(e *Entry) error {
  169. h.F()
  170. return nil
  171. }
  172. func TestHookFireOrder(t *testing.T) {
  173. checkers := []string{}
  174. h := LevelHooks{}
  175. h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "first hook") }})
  176. h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "second hook") }})
  177. h.Add(&HookCallFunc{F: func() { checkers = append(checkers, "third hook") }})
  178. h.Fire(InfoLevel, &Entry{})
  179. require.Equal(t, []string{"first hook", "second hook", "third hook"}, checkers)
  180. }