From 0d5fd83396ddc2b81c2ae50a9d2a69e634db5844 Mon Sep 17 00:00:00 2001 From: Reasno Date: Fri, 29 Oct 2021 18:26:17 +0800 Subject: [PATCH] fix(logging): inconsistency between go kit Logger and spanLogger Only log.Valuer passed via log.With are treated as dynamic argument in go kit log. The change makes spanLogger consistent with go kit. --- logging/log.go | 22 +++++----------------- logging/log_test.go | 4 ++-- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/logging/log.go b/logging/log.go index 859fa6d8..96b4c0d9 100644 --- a/logging/log.go +++ b/logging/log.go @@ -106,14 +106,14 @@ type spanLogger struct { } func (s spanLogger) Log(keyvals ...interface{}) error { - s.kvs = append(s.kvs, keyvals...) for k := range s.kvs { if f, ok := s.kvs[k].(log.Valuer); ok { s.kvs[k] = f() } } + s.kvs = append(s.kvs, keyvals...) s.span.LogKV(s.kvs...) - return s.base.Log(s.kvs...) + return s.base.Log(keyvals...) } // WithContext decorates the log.Logger with information form context. If there is an opentracing span @@ -127,27 +127,15 @@ func WithContext(logger log.Logger, ctx context.Context) log.Logger { args = append(args, kv.Key, kv.Val) } + base := log.With(logger, args...) + span := opentracing.SpanFromContext(ctx) if span == nil { - return withContext(logger, ctx) + return base } return spanLogger{span: span, base: logger, kvs: args} } -func withContext(logger log.Logger, ctx context.Context) log.Logger { - var args []interface{} - - bag := ctxmeta.GetBaggage(ctx) - for _, kv := range bag.Slice() { - args = append(args, kv.Key, kv.Val) - } - - return log.With( - logger, - args..., - ) -} - type levelLogger struct { log.Logger } diff --git a/logging/log_test.go b/logging/log_test.go index a302c56e..3f09ade0 100644 --- a/logging/log_test.go +++ b/logging/log_test.go @@ -65,8 +65,8 @@ func TestSpanLogger(t *testing.T) { spanLogger{ span: &mock, base: log.NewNopLogger(), - kvs: []interface{}{"foo", "bar"}, - }.Log("baz", log.Valuer(func() interface{} { return "qux" })) + kvs: []interface{}{"foo", log.Valuer(func() interface{} { return "bar" })}, + }.Log("baz", "qux") assert.Equal(t, []interface{}{"foo", "bar", "baz", "qux"}, mock.received) }