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.

43 lines
1.0 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/tendermint/tendermint/libs/log"
  8. )
  9. func TestLoggerLogsItsErrors(t *testing.T) {
  10. var buf bytes.Buffer
  11. logger := log.NewTMLogger(&buf)
  12. logger.Info("foo", "baz baz", "bar")
  13. msg := strings.TrimSpace(buf.String())
  14. if !strings.Contains(msg, "foo") {
  15. t.Errorf("expected logger msg to contain ErrInvalidKey, got %s", msg)
  16. }
  17. }
  18. func BenchmarkTMLoggerSimple(b *testing.B) {
  19. benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage)
  20. }
  21. func BenchmarkTMLoggerContextual(b *testing.B) {
  22. benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), withInfoMessage)
  23. }
  24. func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) {
  25. lc := logger.With("common_key", "common_value")
  26. b.ReportAllocs()
  27. b.ResetTimer()
  28. for i := 0; i < b.N; i++ {
  29. f(lc)
  30. }
  31. }
  32. var (
  33. baseInfoMessage = func(logger log.Logger) { logger.Info("foo_message", "foo_key", "foo_value") }
  34. withInfoMessage = func(logger log.Logger) { logger.With("a", "b").Info("c", "d", "f") }
  35. )