Tool to help you manage your Grafana dashboards using Git.

entry_test.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package logrus
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestEntryWithError(t *testing.T) {
  9. assert := assert.New(t)
  10. defer func() {
  11. ErrorKey = "error"
  12. }()
  13. err := fmt.Errorf("kaboom at layer %d", 4711)
  14. assert.Equal(err, WithError(err).Data["error"])
  15. logger := New()
  16. logger.Out = &bytes.Buffer{}
  17. entry := NewEntry(logger)
  18. assert.Equal(err, entry.WithError(err).Data["error"])
  19. ErrorKey = "err"
  20. assert.Equal(err, entry.WithError(err).Data["err"])
  21. }
  22. func TestEntryPanicln(t *testing.T) {
  23. errBoom := fmt.Errorf("boom time")
  24. defer func() {
  25. p := recover()
  26. assert.NotNil(t, p)
  27. switch pVal := p.(type) {
  28. case *Entry:
  29. assert.Equal(t, "kaboom", pVal.Message)
  30. assert.Equal(t, errBoom, pVal.Data["err"])
  31. default:
  32. t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
  33. }
  34. }()
  35. logger := New()
  36. logger.Out = &bytes.Buffer{}
  37. entry := NewEntry(logger)
  38. entry.WithField("err", errBoom).Panicln("kaboom")
  39. }
  40. func TestEntryPanicf(t *testing.T) {
  41. errBoom := fmt.Errorf("boom again")
  42. defer func() {
  43. p := recover()
  44. assert.NotNil(t, p)
  45. switch pVal := p.(type) {
  46. case *Entry:
  47. assert.Equal(t, "kaboom true", pVal.Message)
  48. assert.Equal(t, errBoom, pVal.Data["err"])
  49. default:
  50. t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
  51. }
  52. }()
  53. logger := New()
  54. logger.Out = &bytes.Buffer{}
  55. entry := NewEntry(logger)
  56. entry.WithField("err", errBoom).Panicf("kaboom %v", true)
  57. }