From 79497dd3a59752c1f5b0534dfb90b6f8688bdcc8 Mon Sep 17 00:00:00 2001 From: Tony Chen Date: Tue, 24 May 2022 00:40:45 +0800 Subject: [PATCH] log: fix global logger (#2034) * log: fix global logger --- log/global.go | 2 +- log/helper_test.go | 5 ++++- log/log.go | 21 +++++++-------------- log/log_test.go | 9 --------- log/value.go | 3 +-- 5 files changed, 13 insertions(+), 27 deletions(-) diff --git a/log/global.go b/log/global.go index 0e98bae50..72f871f78 100644 --- a/log/global.go +++ b/log/global.go @@ -38,7 +38,7 @@ func SetLogger(logger Logger) { // GetLogger returns global logger appliance as logger in current process. func GetLogger() Logger { - return global + return global.GetLogger() } // Log Print log by level and keyvals. diff --git a/log/helper_test.go b/log/helper_test.go index dc888a39a..0cb4ba87f 100644 --- a/log/helper_test.go +++ b/log/helper_test.go @@ -73,7 +73,10 @@ func TestContext(t *testing.T) { func Trace() Valuer { return func(ctx context.Context) interface{} { - s := ctx.Value(traceKey{}).(string) + s, ok := ctx.Value(traceKey{}).(string) + if !ok { + return nil + } return s } } diff --git a/log/log.go b/log/log.go index 2a290b707..264dd8c45 100644 --- a/log/log.go +++ b/log/log.go @@ -14,7 +14,7 @@ type Logger interface { } type logger struct { - logs []Logger + logger Logger prefix []interface{} hasValuer bool ctx context.Context @@ -27,10 +27,8 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error { bindValues(c.ctx, kvs) } kvs = append(kvs, keyvals...) - for _, l := range c.logs { - if err := l.Log(level, kvs...); err != nil { - return err - } + if err := c.logger.Log(level, kvs...); err != nil { + return err } return nil } @@ -39,13 +37,13 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error { func With(l Logger, kv ...interface{}) Logger { c, ok := l.(*logger) if !ok { - return &logger{logs: []Logger{l}, prefix: kv, hasValuer: containsValuer(kv)} + return &logger{logger: l, prefix: kv, hasValuer: containsValuer(kv), ctx: context.Background()} } kvs := make([]interface{}, 0, len(c.prefix)+len(kv)) kvs = append(kvs, kv...) kvs = append(kvs, c.prefix...) return &logger{ - logs: c.logs, + logger: l, prefix: kvs, hasValuer: containsValuer(kvs), ctx: c.ctx, @@ -57,17 +55,12 @@ func With(l Logger, kv ...interface{}) Logger { func WithContext(ctx context.Context, l Logger) Logger { c, ok := l.(*logger) if !ok { - return &logger{logs: []Logger{l}, ctx: ctx} + return &logger{logger: l, ctx: ctx} } return &logger{ - logs: c.logs, + logger: l, prefix: c.prefix, hasValuer: c.hasValuer, ctx: ctx, } } - -// MultiLogger wraps multi logger. -func MultiLogger(logs ...Logger) Logger { - return &logger{logs: logs} -} diff --git a/log/log_test.go b/log/log_test.go index dea00f4d6..542bb79d1 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -2,7 +2,6 @@ package log import ( "context" - "os" "testing" ) @@ -12,14 +11,6 @@ func TestInfo(t *testing.T) { _ = logger.Log(LevelInfo, "key1", "value1") } -func TestWrapper(t *testing.T) { - out := NewStdLogger(os.Stdout) - err := NewStdLogger(os.Stderr) - - l := With(MultiLogger(out, err), "ts", DefaultTimestamp, "caller", DefaultCaller) - _ = l.Log(LevelInfo, "msg", "test") -} - func TestWithContext(t *testing.T) { WithContext(context.Background(), nil) } diff --git a/log/value.go b/log/value.go index 86e915597..8154ae934 100644 --- a/log/value.go +++ b/log/value.go @@ -9,9 +9,8 @@ import ( ) var ( - defaultDepth = 3 // DefaultCaller is a Valuer that returns the file and line. - DefaultCaller = Caller(defaultDepth) + DefaultCaller = Caller(3) // DefaultTimestamp is a Valuer that returns the current wallclock time. DefaultTimestamp = Timestamp(time.RFC3339)