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.

94 lines
2.7 KiB

  1. package flags_test
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. tmflags "github.com/tendermint/tendermint/libs/cli/flags"
  7. "github.com/tendermint/tendermint/libs/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. buf.Reset()
  44. logger.With("module", "mempool").With("module", "wire").Debug("Kingpin")
  45. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[0] != have {
  46. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[0], have, c.lvl)
  47. }
  48. buf.Reset()
  49. logger.With("module", "mempool").Info("Kitty Pryde")
  50. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[1] != have {
  51. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[1], have, c.lvl)
  52. }
  53. buf.Reset()
  54. logger.With("module", "mempool").Error("Mesmero")
  55. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[2] != have {
  56. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[2], have, c.lvl)
  57. }
  58. buf.Reset()
  59. logger.With("module", "state").Info("Mind")
  60. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[3] != have {
  61. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[3], have, c.lvl)
  62. }
  63. buf.Reset()
  64. logger.Debug("Gideon")
  65. if have := strings.TrimSpace(buf.String()); c.expectedLogLines[4] != have {
  66. t.Errorf("\nwant '%s'\nhave '%s'\nlevel '%s'", c.expectedLogLines[4], have, c.lvl)
  67. }
  68. }
  69. incorrectLogLevel := []string{"some", "mempool:some", "*:some,mempool:error"}
  70. for _, lvl := range incorrectLogLevel {
  71. if _, err := tmflags.ParseLogLevel(lvl, jsonLogger, defaultLogLevelValue); err == nil {
  72. t.Fatalf("Expected %s to produce error", lvl)
  73. }
  74. }
  75. }