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.

120 lines
3.3 KiB

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