From 4b989151ed7008c2412b94ed37bf4c974b33b6f7 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Mon, 6 Nov 2017 14:18:42 -0500 Subject: [PATCH] log logger's errors (Refs #29) --- log/tm_logger.go | 15 ++++++++++++--- log/tm_logger_test.go | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/log/tm_logger.go b/log/tm_logger.go index dc6932dd8..d49e8d22b 100644 --- a/log/tm_logger.go +++ b/log/tm_logger.go @@ -52,19 +52,28 @@ func NewTMLoggerWithColorFn(w io.Writer, colorFn func(keyvals ...interface{}) te // Info logs a message at level Info. func (l *tmLogger) Info(msg string, keyvals ...interface{}) { lWithLevel := kitlevel.Info(l.srcLogger) - kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...) + if err := kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...); err != nil { + errLogger := kitlevel.Error(l.srcLogger) + kitlog.With(errLogger, msgKey, msg).Log("err", err) + } } // Debug logs a message at level Debug. func (l *tmLogger) Debug(msg string, keyvals ...interface{}) { lWithLevel := kitlevel.Debug(l.srcLogger) - kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...) + if err := kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...); err != nil { + errLogger := kitlevel.Error(l.srcLogger) + kitlog.With(errLogger, msgKey, msg).Log("err", err) + } } // Error logs a message at level Error. func (l *tmLogger) Error(msg string, keyvals ...interface{}) { lWithLevel := kitlevel.Error(l.srcLogger) - kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...) + lWithMsg := kitlog.With(lWithLevel, msgKey, msg) + if err := lWithMsg.Log(keyvals...); err != nil { + lWithMsg.Log("err", err) + } } // With returns a new contextual logger with keyvals prepended to those passed diff --git a/log/tm_logger_test.go b/log/tm_logger_test.go index 8cd2f8274..b2b600ad2 100644 --- a/log/tm_logger_test.go +++ b/log/tm_logger_test.go @@ -1,12 +1,26 @@ package log_test import ( + "bytes" "io/ioutil" + "strings" "testing" + "github.com/go-logfmt/logfmt" "github.com/tendermint/tmlibs/log" ) +func TestLoggerLogsItsErrors(t *testing.T) { + var buf bytes.Buffer + + logger := log.NewTMLogger(&buf) + logger.Info("foo", "baz baz", "bar") + msg := strings.TrimSpace(buf.String()) + if !strings.Contains(msg, logfmt.ErrInvalidKey.Error()) { + t.Errorf("Expected logger msg to contain ErrInvalidKey, got %s", msg) + } +} + func BenchmarkTMLoggerSimple(b *testing.B) { benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage) }