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.

118 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. logger := log.NewTMJSONLogger(&buf)
  69. logger = log.NewFilter(logger, log.AllowError())
  70. logger = logger.With("context", "value")
  71. logger.Error("foo", "bar", "baz")
  72. if want, have := `{"_msg":"foo","bar":"baz","context":"value","level":"error"}`, strings.TrimSpace(buf.String()); want != have {
  73. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  74. }
  75. buf.Reset()
  76. logger.Info("foo", "bar", "baz")
  77. if want, have := ``, strings.TrimSpace(buf.String()); want != have {
  78. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  79. }
  80. }
  81. func TestVariousAllowWith(t *testing.T) {
  82. var buf bytes.Buffer
  83. logger := log.NewTMJSONLogger(&buf)
  84. logger1 := log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("context", "value"))
  85. logger1.With("context", "value").Info("foo", "bar", "baz")
  86. if want, have := `{"_msg":"foo","bar":"baz","context":"value","level":"info"}`, strings.TrimSpace(buf.String()); want != have {
  87. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  88. }
  89. buf.Reset()
  90. logger2 := log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("context", "value"), log.AllowNoneWith("user", "Sam"))
  91. logger2.With("context", "value", "user", "Sam").Info("foo", "bar", "baz")
  92. if want, have := ``, strings.TrimSpace(buf.String()); want != have {
  93. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  94. }
  95. buf.Reset()
  96. logger3 := log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("context", "value"), log.AllowNoneWith("user", "Sam"))
  97. logger3.With("user", "Sam").With("context", "value").Info("foo", "bar", "baz")
  98. if want, have := `{"_msg":"foo","bar":"baz","context":"value","level":"info","user":"Sam"}`, strings.TrimSpace(buf.String()); want != have {
  99. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  100. }
  101. }