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.

42 lines
1.5 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/tmlibs/log"
  10. )
  11. func TestTracingLogger(t *testing.T) {
  12. var buf bytes.Buffer
  13. var logger log.Logger
  14. logger = log.NewTMJSONLogger(&buf)
  15. logger1 := log.NewTracingLogger(logger)
  16. err1 := errors.New("Courage is grace under pressure.")
  17. err2 := errors.New("It does not matter how slowly you go, so long as you do not stop.")
  18. logger1.With("err1", err1).Info("foo", "err2", err2)
  19. have := strings.Replace(strings.Replace(strings.TrimSpace(buf.String()), "\\n", "", -1), "\\t", "", -1)
  20. if want := strings.Replace(strings.Replace(`{"_msg":"foo","err1":"`+fmt.Sprintf("%+v", err1)+`","err2":"`+fmt.Sprintf("%+v", err2)+`","level":"info"}`, "\t", "", -1), "\n", "", -1); want != have {
  21. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  22. }
  23. buf.Reset()
  24. logger.With("err1", stderr.New("Opportunities don't happen. You create them.")).Info("foo", "err2", stderr.New("Once you choose hope, anything's possible."))
  25. if want, have := `{"_msg":"foo","err1":"Opportunities don't happen. You create them.","err2":"Once you choose hope, anything's possible.","level":"info"}`, strings.TrimSpace(buf.String()); want != have {
  26. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  27. }
  28. buf.Reset()
  29. logger.With("user", "Sam").With("context", "value").Info("foo", "bar", "baz")
  30. if want, have := `{"_msg":"foo","bar":"baz","context":"value","level":"info","user":"Sam"}`, strings.TrimSpace(buf.String()); want != have {
  31. t.Errorf("\nwant '%s'\nhave '%s'", want, have)
  32. }
  33. }