From 3445f3ea8eb6ec8b951e0ea9658a355379f4e430 Mon Sep 17 00:00:00 2001 From: freezeChen <453430397@qq.com> Date: Fri, 16 Jun 2023 19:45:58 +0800 Subject: [PATCH] fix: logging middle don't get trace_id value when logger is filterLogger (#2869) * Description (what this PR does / why we need it): fix logging middle don't get trace_id value when logger is filterLogger Which issue(s) this PR fixes (resolves / be part of): #2570 #2571 * format * fix:remove unuse test --- log/filter.go | 6 ++++++ log/filter_test.go | 32 ++++++++++++++++++++++++++++++++ log/log.go | 28 +++++++++++++++++++++------- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/log/filter.go b/log/filter.go index 993fa926e..7ef96655d 100644 --- a/log/filter.go +++ b/log/filter.go @@ -1,5 +1,7 @@ package log +import "context" + // FilterOption is filter option. type FilterOption func(*Filter) @@ -39,6 +41,7 @@ func FilterFunc(f func(level Level, keyvals ...interface{}) bool) FilterOption { // Filter is a logger filter. type Filter struct { + ctx context.Context logger Logger level Level key map[interface{}]struct{} @@ -67,6 +70,9 @@ func (f *Filter) Log(level Level, keyvals ...interface{}) error { // prefixkv contains the slice of arguments defined as prefixes during the log initialization var prefixkv []interface{} l, ok := f.logger.(*logger) + if ok { + l.ctx = f.ctx + } if ok && len(l.prefix) > 0 { prefixkv = make([]interface{}, 0, len(l.prefix)) prefixkv = append(prefixkv, l.prefix...) diff --git a/log/filter_test.go b/log/filter_test.go index 9258400c3..679fbe8c4 100644 --- a/log/filter_test.go +++ b/log/filter_test.go @@ -2,7 +2,9 @@ package log import ( "bytes" + "context" "io" + "strings" "testing" ) @@ -140,3 +142,33 @@ func testFilterFuncWithLoggerPrefix(level Level, keyvals ...interface{}) bool { } return false } + +func TestFilterWithContext(t *testing.T) { + ctxKey := struct{}{} + ctxValue := "filter test value" + + v1 := func() Valuer { + return func(ctx context.Context) interface{} { + return ctx.Value(ctxKey) + } + } + + info := &bytes.Buffer{} + + logger := With(NewStdLogger(info), "request_id", v1()) + filter := NewFilter(logger, FilterLevel(LevelError)) + + ctx := context.WithValue(context.Background(), ctxKey, ctxValue) + + _ = WithContext(ctx, filter).Log(LevelInfo, "kind", "test") + + if info.String() != "" { + t.Error("filter is not woring") + return + } + + _ = WithContext(ctx, filter).Log(LevelError, "kind", "test") + if !strings.Contains(info.String(), ctxValue) { + t.Error("don't read ctx value") + } +} diff --git a/log/log.go b/log/log.go index 5022ba4bb..38cef8052 100644 --- a/log/log.go +++ b/log/log.go @@ -51,13 +51,27 @@ func With(l Logger, kv ...interface{}) Logger { // to ctx. The provided ctx must be non-nil. func WithContext(ctx context.Context, l Logger) Logger { c, ok := l.(*logger) - if !ok { - return &logger{logger: l, ctx: ctx} + if ok { + return &logger{ + logger: c.logger, + prefix: c.prefix, + hasValuer: c.hasValuer, + ctx: ctx, + } } - return &logger{ - logger: c.logger, - prefix: c.prefix, - hasValuer: c.hasValuer, - ctx: ctx, + + f, ok := l.(*Filter) + if ok { + f.ctx = ctx + return &Filter{ + ctx: ctx, + logger: f.logger, + level: f.level, + key: f.key, + value: f.value, + filter: f.filter, + } } + + return &logger{logger: l, ctx: ctx} }