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.

entry_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package logrus
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestEntryWithError(t *testing.T) {
  11. assert := assert.New(t)
  12. defer func() {
  13. ErrorKey = "error"
  14. }()
  15. err := fmt.Errorf("kaboom at layer %d", 4711)
  16. assert.Equal(err, WithError(err).Data["error"])
  17. logger := New()
  18. logger.Out = &bytes.Buffer{}
  19. entry := NewEntry(logger)
  20. assert.Equal(err, entry.WithError(err).Data["error"])
  21. ErrorKey = "err"
  22. assert.Equal(err, entry.WithError(err).Data["err"])
  23. }
  24. func TestEntryWithContext(t *testing.T) {
  25. assert := assert.New(t)
  26. ctx := context.WithValue(context.Background(), "foo", "bar")
  27. assert.Equal(ctx, WithContext(ctx).Context)
  28. logger := New()
  29. logger.Out = &bytes.Buffer{}
  30. entry := NewEntry(logger)
  31. assert.Equal(ctx, entry.WithContext(ctx).Context)
  32. }
  33. func TestEntryPanicln(t *testing.T) {
  34. errBoom := fmt.Errorf("boom time")
  35. defer func() {
  36. p := recover()
  37. assert.NotNil(t, p)
  38. switch pVal := p.(type) {
  39. case *Entry:
  40. assert.Equal(t, "kaboom", pVal.Message)
  41. assert.Equal(t, errBoom, pVal.Data["err"])
  42. default:
  43. t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
  44. }
  45. }()
  46. logger := New()
  47. logger.Out = &bytes.Buffer{}
  48. entry := NewEntry(logger)
  49. entry.WithField("err", errBoom).Panicln("kaboom")
  50. }
  51. func TestEntryPanicf(t *testing.T) {
  52. errBoom := fmt.Errorf("boom again")
  53. defer func() {
  54. p := recover()
  55. assert.NotNil(t, p)
  56. switch pVal := p.(type) {
  57. case *Entry:
  58. assert.Equal(t, "kaboom true", pVal.Message)
  59. assert.Equal(t, errBoom, pVal.Data["err"])
  60. default:
  61. t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
  62. }
  63. }()
  64. logger := New()
  65. logger.Out = &bytes.Buffer{}
  66. entry := NewEntry(logger)
  67. entry.WithField("err", errBoom).Panicf("kaboom %v", true)
  68. }
  69. const (
  70. badMessage = "this is going to panic"
  71. panicMessage = "this is broken"
  72. )
  73. type panickyHook struct{}
  74. func (p *panickyHook) Levels() []Level {
  75. return []Level{InfoLevel}
  76. }
  77. func (p *panickyHook) Fire(entry *Entry) error {
  78. if entry.Message == badMessage {
  79. panic(panicMessage)
  80. }
  81. return nil
  82. }
  83. func TestEntryHooksPanic(t *testing.T) {
  84. logger := New()
  85. logger.Out = &bytes.Buffer{}
  86. logger.Level = InfoLevel
  87. logger.Hooks.Add(&panickyHook{})
  88. defer func() {
  89. p := recover()
  90. assert.NotNil(t, p)
  91. assert.Equal(t, panicMessage, p)
  92. entry := NewEntry(logger)
  93. entry.Info("another message")
  94. }()
  95. entry := NewEntry(logger)
  96. entry.Info(badMessage)
  97. }
  98. func TestEntryWithIncorrectField(t *testing.T) {
  99. assert := assert.New(t)
  100. fn := func() {}
  101. e := Entry{}
  102. eWithFunc := e.WithFields(Fields{"func": fn})
  103. eWithFuncPtr := e.WithFields(Fields{"funcPtr": &fn})
  104. assert.Equal(eWithFunc.err, `can not add field "func"`)
  105. assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
  106. eWithFunc = eWithFunc.WithField("not_a_func", "it is a string")
  107. eWithFuncPtr = eWithFuncPtr.WithField("not_a_func", "it is a string")
  108. assert.Equal(eWithFunc.err, `can not add field "func"`)
  109. assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
  110. eWithFunc = eWithFunc.WithTime(time.Now())
  111. eWithFuncPtr = eWithFuncPtr.WithTime(time.Now())
  112. assert.Equal(eWithFunc.err, `can not add field "func"`)
  113. assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
  114. }
  115. func TestEntryLogfLevel(t *testing.T) {
  116. logger := New()
  117. buffer := &bytes.Buffer{}
  118. logger.Out = buffer
  119. logger.SetLevel(InfoLevel)
  120. entry := NewEntry(logger)
  121. entry.Logf(DebugLevel, "%s", "debug")
  122. assert.NotContains(t, buffer.String(), "debug", )
  123. entry.Logf(WarnLevel, "%s", "warn")
  124. assert.Contains(t, buffer.String(), "warn", )
  125. }