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.

103 lines
2.5 KiB

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.AllowAll())
  86. logger = logger.With("context", "value")
  87. logger.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. }