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.

138 lines
3.8 KiB

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