Skip to content

Commit

Permalink
chore(log): reduce span log
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Aug 25, 2021
1 parent 21d1a4d commit 706772e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func (t defaultLogger) For(ctx context.Context) Logger {
l := spanLogger{span: span, logger: t.logger.WithOptions(zap.AddCallerSkip(t.skip())), additionalFields: t.additionalFields}

if jaegerCtx, ok := span.Context().(jaeger.SpanContext); ok {
l.spanFields = []zapcore.Field{
l.logger = l.logger.With(
zap.String("trace_id", jaegerCtx.TraceID().String()),
zap.String("span_id", jaegerCtx.SpanID().String()),
}
)
}

return l
Expand Down
18 changes: 5 additions & 13 deletions pkg/log/spanlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
type spanLogger struct {
logger *zap.Logger
span opentracing.Span
spanFields []zapcore.Field
additionalFields []zapcore.Field
}

Expand All @@ -28,44 +27,37 @@ func (t spanLogger) Named(name string) Logger {

// Debug log message
func (t spanLogger) Debug(msg string, fields ...zapcore.Field) {
if t.logger.Core().Enabled(zap.DebugLevel) {
t.logToSpan("debug", msg, append(t.additionalFields, fields...)...)
}
t.logger.Debug(msg, append(t.spanFields, fields...)...)
t.logger.Debug(msg, fields...)
}

// Info log message
func (t spanLogger) Info(msg string, fields ...zapcore.Field) {
if t.logger.Core().Enabled(zap.InfoLevel) {
t.logToSpan("info", msg, append(t.additionalFields, fields...)...)
}
t.logger.Info(msg, append(t.spanFields, fields...)...)
t.logger.Info(msg, fields...)
}

// Warn log message
func (t spanLogger) Warn(msg string, fields ...zapcore.Field) {
t.logToSpan("warn", msg, append(t.additionalFields, fields...)...)
t.logger.Warn(msg, append(t.spanFields, fields...)...)
t.logger.Warn(msg, fields...)
}

// Error log message
func (t spanLogger) Error(msg string, fields ...zapcore.Field) {
t.logToSpan("error", msg, fields...)
t.logger.Error(msg, append(t.spanFields, fields...)...)
t.logger.Error(msg, fields...)
}

// Fatal log message
func (t spanLogger) Fatal(msg string, fields ...zapcore.Field) {
t.logToSpan("fatal", msg, append(t.additionalFields, fields...)...)
t.logger.Fatal(msg, append(t.spanFields, fields...)...)
t.logger.Fatal(msg, fields...)
}

// With add zap fields
func (t spanLogger) With(fields ...zapcore.Field) Logger {
return spanLogger{
logger: t.logger.With(fields...),
span: t.span,
spanFields: t.spanFields,
additionalFields: append(t.additionalFields, fields...),
}
}
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package xservice

const Version = "v1.0.18"
const Version = "v1.0.19"

0 comments on commit 706772e

Please sign in to comment.