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.

44 lines
1.1 KiB

8 years ago
8 years ago
  1. package log_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "strings"
  6. "testing"
  7. "github.com/go-logfmt/logfmt"
  8. "github.com/tendermint/tmlibs/log"
  9. )
  10. func TestLoggerLogsItsErrors(t *testing.T) {
  11. var buf bytes.Buffer
  12. logger := log.NewTMLogger(&buf)
  13. logger.Info("foo", "baz baz", "bar")
  14. msg := strings.TrimSpace(buf.String())
  15. if !strings.Contains(msg, logfmt.ErrInvalidKey.Error()) {
  16. t.Errorf("Expected logger msg to contain ErrInvalidKey, got %s", msg)
  17. }
  18. }
  19. func BenchmarkTMLoggerSimple(b *testing.B) {
  20. benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage)
  21. }
  22. func BenchmarkTMLoggerContextual(b *testing.B) {
  23. benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), withInfoMessage)
  24. }
  25. func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) {
  26. lc := logger.With("common_key", "common_value")
  27. b.ReportAllocs()
  28. b.ResetTimer()
  29. for i := 0; i < b.N; i++ {
  30. f(lc)
  31. }
  32. }
  33. var (
  34. baseInfoMessage = func(logger log.Logger) { logger.Info("foo_message", "foo_key", "foo_value") }
  35. withInfoMessage = func(logger log.Logger) { logger.With("a", "b").Info("c", "d", "f") }
  36. )