From b2bd039923c20f59d13c7992a079d95e92f328fe Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 4 May 2017 17:33:32 +0400 Subject: [PATCH] correct impl of WithLevel --- log/logger.go | 22 ++++++++++++++++++++++ log/tm_logger.go | 14 -------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/log/logger.go b/log/logger.go index 790bcad74..a895aed6d 100644 --- a/log/logger.go +++ b/log/logger.go @@ -4,6 +4,7 @@ import ( "fmt" kitlog "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" ) // Logger is what any Tendermint library should take. @@ -25,3 +26,24 @@ func With(logger Logger, keyvals ...interface{}) Logger { panic(fmt.Sprintf("Unexpected logger of type %T", logger)) } } + +// WithLevel returns a copy of the logger with a level set to lvl. +func WithLevel(logger Logger, lvl string) Logger { + switch logger.(type) { + case *tmLogger: + switch lvl { + case "info": + return &tmLogger{level.NewFilter(logger.(*tmLogger).srcLogger, level.AllowInfo())} + case "debug": + return &tmLogger{level.NewFilter(logger.(*tmLogger).srcLogger, level.AllowDebug())} + case "error": + return &tmLogger{level.NewFilter(logger.(*tmLogger).srcLogger, level.AllowError())} + default: + panic(fmt.Sprintf("Unexpected level %v, expect either \"info\" or \"debug\" or \"error\"", lvl)) + } + case *nopLogger: + return logger + default: + panic(fmt.Sprintf("Unexpected logger of type %T", logger)) + } +} diff --git a/log/tm_logger.go b/log/tm_logger.go index f12442db1..d8550ea67 100644 --- a/log/tm_logger.go +++ b/log/tm_logger.go @@ -41,20 +41,6 @@ func NewTMLogger(w io.Writer) Logger { return &tmLogger{srcLogger} } -// WithLevel returns a copy of the logger with a level set to lvl. -func (l *tmLogger) WithLevel(lvl string) Logger { - switch lvl { - case "info": - return &tmLogger{level.NewFilter(l.srcLogger, level.AllowInfo())} - case "debug": - return &tmLogger{level.NewFilter(l.srcLogger, level.AllowDebug())} - case "error": - return &tmLogger{level.NewFilter(l.srcLogger, level.AllowError())} - default: - panic(fmt.Sprintf("Unexpected level %v, expect either \"info\" or \"debug\" or \"error\"", lvl)) - } -} - // Info logs a message at level Info. func (l *tmLogger) Info(msg string, keyvals ...interface{}) error { lWithLevel := level.Info(l.srcLogger)