Skip to content

Commit

Permalink
Merge pull request #56 from rogerogers/fix/zap
Browse files Browse the repository at this point in the history
fix: two lines of log when no provider
  • Loading branch information
GuangmingLuo authored Jul 5, 2024
2 parents d3c089f + 631c2b1 commit 9abdf96
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
3 changes: 1 addition & 2 deletions logging/zap/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.19.0
go.opentelemetry.io/otel/sdk v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
go.uber.org/zap v1.26.0
go.uber.org/zap v1.27.0
)

require (
Expand All @@ -22,7 +22,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.uber.org/goleak v1.2.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
Expand Down
7 changes: 3 additions & 4 deletions logging/zap/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJ
go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
27 changes: 13 additions & 14 deletions logging/zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,33 @@ func (l *Logger) CtxLogf(level hlog.Level, ctx context.Context, format string, k
var zlevel zapcore.Level
span := trace.SpanFromContext(ctx)

ctx = context.WithValue(ctx, hertzzap.ExtraKey(traceIDKey), span.SpanContext().TraceID())
ctx = context.WithValue(ctx, hertzzap.ExtraKey(spanIDKey), span.SpanContext().SpanID())
ctx = context.WithValue(ctx, hertzzap.ExtraKey(traceFlagsKey), span.SpanContext().TraceFlags())
if span.SpanContext().IsValid() {
ctx = context.WithValue(ctx, hertzzap.ExtraKey(traceIDKey), span.SpanContext().TraceID())
ctx = context.WithValue(ctx, hertzzap.ExtraKey(spanIDKey), span.SpanContext().SpanID())
ctx = context.WithValue(ctx, hertzzap.ExtraKey(traceFlagsKey), span.SpanContext().TraceFlags())

l.Logger.CtxLogf(level, ctx, format, kvs...)
} else {
l.Logger.Logf(level, format, kvs...)
}

if !span.IsRecording() {
return
}

switch level {
case hlog.LevelDebug, hlog.LevelTrace:
zlevel = zap.DebugLevel
l.Logger.CtxDebugf(ctx, format, kvs...)
case hlog.LevelInfo:
zlevel = zap.InfoLevel
l.Logger.CtxInfof(ctx, format, kvs...)
case hlog.LevelNotice, hlog.LevelWarn:
zlevel = zap.WarnLevel
l.Logger.CtxWarnf(ctx, format, kvs...)
case hlog.LevelError:
zlevel = zap.ErrorLevel
l.Logger.CtxErrorf(ctx, format, kvs...)
case hlog.LevelFatal:
zlevel = zap.FatalLevel
l.Logger.CtxFatalf(ctx, format, kvs...)
default:
zlevel = zap.WarnLevel
l.Logger.CtxWarnf(ctx, format, kvs...)
}

if !span.IsRecording() {
l.Logger.Logf(level, format, kvs...)
return
}

// set span status
Expand Down

0 comments on commit 9abdf96

Please sign in to comment.