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.

96 lines
2.7 KiB

  1. package flags_test
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. tmflags "github.com/tendermint/tmlibs/cli/flags"
  7. "github.com/tendermint/tmlibs/log"
  8. )
  9. const (
  10. defaultLogLevelValue = "info"
  11. )
  12. func TestParseLogLevel(t *testing.T) {
  13. var buf bytes.Buffer
  14. jsonLogger := log.NewTMJSONLogger(&buf)
  15. correctLogLevels := []struct {
  16. lvl string
  17. expectedLogLines []string
  18. }{
  19. {"mempool:error", []string{
  20. ``, // if no default is given, assume info
  21. ``,
  22. `{"_msg":"Mesmero","level":"error","module":"mempool"}`,
  23. `{"_msg":"Mind","level":"info","module":"state"}`, // if no default is given, assume info
  24. ``}},
  25. {"mempool:error,*:debug", []string{
  26. `{"_msg":"Kingpin","level":"debug","module":"wire"}`,
  27. ``,
  28. `{"_msg":"Mesmero","level":"error","module":"mempool"}`,
  29. `{"_msg":"Mind","level":"info","module":"state"}`,
  30. `{"_msg":"Gideon","level":"debug"}`}},
  31. {"*:debug,wire:none", []string{
  32. ``,
  33. `{"_msg":"Kitty Pryde","level":"info","module":"mempool"}`,
  34. `{"_msg":"Mesmero","level":"error","module":"mempool"}`,
  35. `{"_msg":"Mind","level":"info","module":"state"}`,
  36. `{"_msg":"Gideon","level":"debug"}`}},
  37. }
  38. for _, c := range correctLogLevels {
  39. logger, err := tmflags.ParseLogLevel(c.lvl, jsonLogger, defaultLogLevelValue)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. logger = logger
  44. buf.Reset()
  45. logger.With("module", "wire").Debug("Kingpin")
  46. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[0] != have {
  47. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[0], have, c.lvl)
  48. }
  49. buf.Reset()
  50. logger.With("module", "mempool").Info("Kitty Pryde")
  51. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[1] != have {
  52. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[1], have, c.lvl)
  53. }
  54. buf.Reset()
  55. logger.With("module", "mempool").Error("Mesmero")
  56. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[2] != have {
  57. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[2], have, c.lvl)
  58. }
  59. buf.Reset()
  60. logger.With("module", "state").Info("Mind")
  61. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[3] != have {
  62. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[3], have, c.lvl)
  63. }
  64. buf.Reset()
  65. logger.Debug("Gideon")
  66. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[4] != have {
  67. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[4], have, c.lvl)
  68. }
  69. }
  70. incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"}
  71. for _, lvl := range incorrectLogLevel {
  72. if _, err := tmflags.ParseLogLevel(lvl, jsonLogger, defaultLogLevelValue); err == nil {
  73. t.Fatalf("Expected %s to produce error", lvl)
  74. }
  75. }
  76. }