Skip to content

Commit

Permalink
perf(zap): perf improve (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <[email protected]>
  • Loading branch information
rogerogers authored Jan 28, 2024
1 parent acc7b4e commit 6b18af4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,23 @@ func (l *Logger) Logf(level hlog.Level, format string, kvs ...interface{}) {
}

func (l *Logger) CtxLogf(level hlog.Level, ctx context.Context, format string, kvs ...interface{}) {
log := l.l.Sugar()
zapLogger := l.l
if len(l.config.extraKeys) > 0 {
for _, k := range l.config.extraKeys {
if l.config.extraKeyAsStr {
log = log.With(string(k), ctx.Value(string(k)))
v := ctx.Value(string(k))
if v != nil {
zapLogger = zapLogger.With(zap.Any(string(k), v))
}
} else {
log = log.With(string(k), ctx.Value(k))
v := ctx.Value(k)
if v != nil {
zapLogger = zapLogger.With(zap.Any(string(k), v))
}
}
}
}
log := zapLogger.Sugar()
switch level {
case hlog.LevelDebug, hlog.LevelTrace:
log.Debugf(format, kvs...)
Expand Down

0 comments on commit 6b18af4

Please sign in to comment.