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.

64 lines
1.6 KiB

  1. package log_test
  2. import (
  3. "bytes"
  4. stderr "errors"
  5. "fmt"
  6. "strings"
  7. "testing"
  8. "github.com/pkg/errors"
  9. "github.com/tendermint/tendermint/libs/log"
  10. )
  11. func TestTracingLogger(t *testing.T) {
  12. var buf bytes.Buffer
  13. logger := log.NewTMJSONLogger(&buf)
  14. logger1 := log.NewTracingLogger(logger)
  15. err1 := errors.New("Courage is grace under pressure.")
  16. err2 := errors.New("It does not matter how slowly you go, so long as you do not stop.")
  17. logger1.With("err1", err1).Info("foo", "err2", err2)
  18. want := strings.Replace(
  19. strings.Replace(
  20. `{"_msg":"foo","err1":"`+
  21. fmt.Sprintf("%+v", err1)+
  22. `","err2":"`+
  23. fmt.Sprintf("%+v", err2)+
  24. `","level":"info"}`,
  25. "\t", "", -1,
  26. ), "\n", "", -1)
  27. have := strings.Replace(strings.Replace(strings.TrimSpace(buf.String()), "\\n", "", -1), "\\t", "", -1)
  28. if want != have {
  29. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  30. }
  31. buf.Reset()
  32. logger.With(
  33. "err1", stderr.New("Opportunities don't happen. You create them."),
  34. ).Info(
  35. "foo", "err2", stderr.New("Once you choose hope, anything's possible."),
  36. )
  37. want = `{"_msg":"foo",` +
  38. `"err1":"Opportunities don't happen. You create them.",` +
  39. `"err2":"Once you choose hope, anything's possible.",` +
  40. `"level":"info"}`
  41. have = strings.TrimSpace(buf.String())
  42. if want != have {
  43. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  44. }
  45. buf.Reset()
  46. logger.With("user", "Sam").With("context", "value").Info("foo", "bar", "baz")
  47. want = `{"_msg":"foo","bar":"baz","context":"value","level":"info","user":"Sam"}`
  48. have = strings.TrimSpace(buf.String())
  49. if want != have {
  50. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  51. }
  52. }