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.

144 lines
2.5 KiB

  1. package logrus
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. type TestHook struct {
  8. Fired bool
  9. }
  10. func (hook *TestHook) Fire(entry *Entry) error {
  11. hook.Fired = true
  12. return nil
  13. }
  14. func (hook *TestHook) Levels() []Level {
  15. return []Level{
  16. DebugLevel,
  17. InfoLevel,
  18. WarnLevel,
  19. ErrorLevel,
  20. FatalLevel,
  21. PanicLevel,
  22. }
  23. }
  24. func TestHookFires(t *testing.T) {
  25. hook := new(TestHook)
  26. LogAndAssertJSON(t, func(log *Logger) {
  27. log.Hooks.Add(hook)
  28. assert.Equal(t, hook.Fired, false)
  29. log.Print("test")
  30. }, func(fields Fields) {
  31. assert.Equal(t, hook.Fired, true)
  32. })
  33. }
  34. type ModifyHook struct {
  35. }
  36. func (hook *ModifyHook) Fire(entry *Entry) error {
  37. entry.Data["wow"] = "whale"
  38. return nil
  39. }
  40. func (hook *ModifyHook) Levels() []Level {
  41. return []Level{
  42. DebugLevel,
  43. InfoLevel,
  44. WarnLevel,
  45. ErrorLevel,
  46. FatalLevel,
  47. PanicLevel,
  48. }
  49. }
  50. func TestHookCanModifyEntry(t *testing.T) {
  51. hook := new(ModifyHook)
  52. LogAndAssertJSON(t, func(log *Logger) {
  53. log.Hooks.Add(hook)
  54. log.WithField("wow", "elephant").Print("test")
  55. }, func(fields Fields) {
  56. assert.Equal(t, fields["wow"], "whale")
  57. })
  58. }
  59. func TestCanFireMultipleHooks(t *testing.T) {
  60. hook1 := new(ModifyHook)
  61. hook2 := new(TestHook)
  62. LogAndAssertJSON(t, func(log *Logger) {
  63. log.Hooks.Add(hook1)
  64. log.Hooks.Add(hook2)
  65. log.WithField("wow", "elephant").Print("test")
  66. }, func(fields Fields) {
  67. assert.Equal(t, fields["wow"], "whale")
  68. assert.Equal(t, hook2.Fired, true)
  69. })
  70. }
  71. type ErrorHook struct {
  72. Fired bool
  73. }
  74. func (hook *ErrorHook) Fire(entry *Entry) error {
  75. hook.Fired = true
  76. return nil
  77. }
  78. func (hook *ErrorHook) Levels() []Level {
  79. return []Level{
  80. ErrorLevel,
  81. }
  82. }
  83. func TestErrorHookShouldntFireOnInfo(t *testing.T) {
  84. hook := new(ErrorHook)
  85. LogAndAssertJSON(t, func(log *Logger) {
  86. log.Hooks.Add(hook)
  87. log.Info("test")
  88. }, func(fields Fields) {
  89. assert.Equal(t, hook.Fired, false)
  90. })
  91. }
  92. func TestErrorHookShouldFireOnError(t *testing.T) {
  93. hook := new(ErrorHook)
  94. LogAndAssertJSON(t, func(log *Logger) {
  95. log.Hooks.Add(hook)
  96. log.Error("test")
  97. }, func(fields Fields) {
  98. assert.Equal(t, hook.Fired, true)
  99. })
  100. }
  101. func TestAddHookRace(t *testing.T) {
  102. var wg sync.WaitGroup
  103. wg.Add(2)
  104. hook := new(ErrorHook)
  105. LogAndAssertJSON(t, func(log *Logger) {
  106. go func() {
  107. defer wg.Done()
  108. log.AddHook(hook)
  109. }()
  110. go func() {
  111. defer wg.Done()
  112. log.Error("test")
  113. }()
  114. wg.Wait()
  115. }, func(fields Fields) {
  116. // the line may have been logged
  117. // before the hook was added, so we can't
  118. // actually assert on the hook
  119. })
  120. }