From 9e208daa71e8c740d6ef2a2b6b03f91085ce18ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=B7=E6=BA=AA?= Date: Mon, 17 Jan 2022 09:21:59 +0800 Subject: [PATCH] feat(logging): add WithBaggage (#225) --- logging/log.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/logging/log.go b/logging/log.go index 8db370aa..bcd230c7 100644 --- a/logging/log.go +++ b/logging/log.go @@ -119,6 +119,15 @@ func (s spanLogger) Log(keyvals ...interface{}) error { // WithContext decorates the log.Logger with information form context. If there is an opentracing span // in the context, the span will receive the logger output as well. func WithContext(logger log.Logger, ctx context.Context) log.Logger { + span := opentracing.SpanFromContext(ctx) + if span == nil { + return WithBaggage(logger, ctx) + } + return WithBaggage(spanLogger{span: span, base: logger}, ctx) +} + +// WithBaggage decorates the log.Logger with information form context. +func WithBaggage(logger log.Logger, ctx context.Context) log.Logger { var args []interface{} bag := ctxmeta.GetBaggage(ctx) @@ -129,11 +138,7 @@ func WithContext(logger log.Logger, ctx context.Context) log.Logger { base := log.With(logger, args...) - span := opentracing.SpanFromContext(ctx) - if span == nil { - return base - } - return spanLogger{span: span, base: logger, kvs: args} + return base } type levelLogger struct {