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
pull/2608/merge
freezeChen 1 year ago committed by GitHub
parent 32c0d2dd97
commit 3445f3ea8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      log/filter.go
  2. 32
      log/filter_test.go
  3. 28
      log/log.go

@ -1,5 +1,7 @@
package log package log
import "context"
// FilterOption is filter option. // FilterOption is filter option.
type FilterOption func(*Filter) type FilterOption func(*Filter)
@ -39,6 +41,7 @@ func FilterFunc(f func(level Level, keyvals ...interface{}) bool) FilterOption {
// Filter is a logger filter. // Filter is a logger filter.
type Filter struct { type Filter struct {
ctx context.Context
logger Logger logger Logger
level Level level Level
key map[interface{}]struct{} 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 // prefixkv contains the slice of arguments defined as prefixes during the log initialization
var prefixkv []interface{} var prefixkv []interface{}
l, ok := f.logger.(*logger) l, ok := f.logger.(*logger)
if ok {
l.ctx = f.ctx
}
if ok && len(l.prefix) > 0 { if ok && len(l.prefix) > 0 {
prefixkv = make([]interface{}, 0, len(l.prefix)) prefixkv = make([]interface{}, 0, len(l.prefix))
prefixkv = append(prefixkv, l.prefix...) prefixkv = append(prefixkv, l.prefix...)

@ -2,7 +2,9 @@ package log
import ( import (
"bytes" "bytes"
"context"
"io" "io"
"strings"
"testing" "testing"
) )
@ -140,3 +142,33 @@ func testFilterFuncWithLoggerPrefix(level Level, keyvals ...interface{}) bool {
} }
return false 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")
}
}

@ -51,13 +51,27 @@ func With(l Logger, kv ...interface{}) Logger {
// to ctx. The provided ctx must be non-nil. // to ctx. The provided ctx must be non-nil.
func WithContext(ctx context.Context, l Logger) Logger { func WithContext(ctx context.Context, l Logger) Logger {
c, ok := l.(*logger) c, ok := l.(*logger)
if !ok { if ok {
return &logger{logger: l, ctx: ctx} return &logger{
logger: c.logger,
prefix: c.prefix,
hasValuer: c.hasValuer,
ctx: ctx,
}
} }
return &logger{
logger: c.logger, f, ok := l.(*Filter)
prefix: c.prefix, if ok {
hasValuer: c.hasValuer, f.ctx = ctx
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}
} }

Loading…
Cancel
Save