Browse Source

log logger's errors (Refs #29)

pull/1842/head
Anton Kaliaev 7 years ago
parent
commit
4b989151ed
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 26 additions and 3 deletions
  1. +12
    -3
      log/tm_logger.go
  2. +14
    -0
      log/tm_logger_test.go

+ 12
- 3
log/tm_logger.go View File

@ -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


+ 14
- 0
log/tm_logger_test.go View File

@ -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)
}


Loading…
Cancel
Save