Skip to content

Commit

Permalink
feat(logging): add WithBaggage (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasno authored Jan 17, 2022
1 parent 6f85ce5 commit 9e208da
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 9e208da

Please sign in to comment.