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.

122 lines
2.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package log_test
  2. import (
  3. "bytes"
  4. "errors"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/tendermint/tmlibs/log"
  9. )
  10. func TestVariousLevels(t *testing.T) {
  11. testCases := []struct {
  12. name string
  13. allowed log.Option
  14. want string
  15. }{
  16. {
  17. "AllowAll",
  18. log.AllowAll(),
  19. strings.Join([]string{
  20. `{"_msg":"here","level":"debug","this is":"debug log"}`,
  21. `{"_msg":"here","level":"info","this is":"info log"}`,
  22. `{"_msg":"here","level":"error","this is":"error log"}`,
  23. }, "\n"),
  24. },
  25. {
  26. "AllowDebug",
  27. log.AllowDebug(),
  28. strings.Join([]string{
  29. `{"_msg":"here","level":"debug","this is":"debug log"}`,
  30. `{"_msg":"here","level":"info","this is":"info log"}`,
  31. `{"_msg":"here","level":"error","this is":"error log"}`,
  32. }, "\n"),
  33. },
  34. {
  35. "AllowInfo",
  36. log.AllowInfo(),
  37. strings.Join([]string{
  38. `{"_msg":"here","level":"info","this is":"info log"}`,
  39. `{"_msg":"here","level":"error","this is":"error log"}`,
  40. }, "\n"),
  41. },
  42. {
  43. "AllowError",
  44. log.AllowError(),
  45. strings.Join([]string{
  46. `{"_msg":"here","level":"error","this is":"error log"}`,
  47. }, "\n"),
  48. },
  49. {
  50. "AllowNone",
  51. log.AllowNone(),
  52. ``,
  53. },
  54. }
  55. for _, tc := range testCases {
  56. t.Run(tc.name, func(t *testing.T) {
  57. var buf bytes.Buffer
  58. logger := log.NewFilter(log.NewTMJSONLogger(&buf), tc.allowed)
  59. logger.Debug("here", "this is", "debug log")
  60. logger.Info("here", "this is", "info log")
  61. logger.Error("here", "this is", "error log")
  62. if want, have := tc.want, strings.TrimSpace(buf.String()); want != have {
  63. t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
  64. }
  65. })
  66. }
  67. }
  68. func TestErrNotAllowed(t *testing.T) {
  69. myError := errors.New("squelched!")
  70. opts := []log.Option{
  71. log.AllowError(),
  72. log.ErrNotAllowed(myError),
  73. }
  74. logger := log.NewFilter(log.NewNopLogger(), opts...)
  75. if want, have := myError, logger.Info("foo", "bar", "baz"); want != have {
  76. t.Errorf("want %#+v, have %#+v", want, have)
  77. }
  78. if want, have := error(nil), logger.Error("foo", "bar", "baz"); want != have {
  79. t.Errorf("want %#+v, have %#+v", want, have)
  80. }
  81. }
  82. func TestLevelContext(t *testing.T) {
  83. var buf bytes.Buffer
  84. var logger log.Logger
  85. logger = log.NewTMJSONLogger(&buf)
  86. logger = log.NewFilter(logger, log.AllowError())
  87. logger = logger.With("context", "value")
  88. logger.Error("foo", "bar", "baz")
  89. if want, have := `{"_msg":"foo","bar":"baz","context":"value","level":"error"}`, strings.TrimSpace(buf.String()); want != have {
  90. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  91. }
  92. buf.Reset()
  93. logger.Info("foo", "bar", "baz")
  94. if want, have := ``, strings.TrimSpace(buf.String()); want != have {
  95. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  96. }
  97. }
  98. func TestNewFilterByLevel(t *testing.T) {
  99. assert := assert.New(t)
  100. var logger log.Logger
  101. logger = log.NewNopLogger()
  102. assert.NotPanics(func() {
  103. logger = log.NewFilterByLevel(logger, "info")
  104. })
  105. assert.Panics(func() {
  106. logger = log.NewFilterByLevel(logger, "smth")
  107. })
  108. }