Skip to content

Commit

Permalink
fix: limit log level to avoid gc alloc memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
FGadvancer committed Dec 26, 2023
1 parent 156d5ac commit f7b0b24
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,25 @@ func (l *ZapLogger) ToZap() *zap.SugaredLogger {
}

func (l *ZapLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
if l.level > zapcore.DebugLevel {
return
}
keysAndValues = l.kvAppend(ctx, keysAndValues)
l.zap.Debugw(msg, keysAndValues...)
}

func (l *ZapLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
if l.level > zapcore.InfoLevel {
return
}
keysAndValues = l.kvAppend(ctx, keysAndValues)
l.zap.Infow(msg, keysAndValues...)
}

func (l *ZapLogger) Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
if l.level > zapcore.WarnLevel {
return
}
if err != nil {
keysAndValues = append(keysAndValues, "error", err.Error())
}
Expand All @@ -247,6 +256,9 @@ func (l *ZapLogger) Warn(ctx context.Context, msg string, err error, keysAndValu
}

func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
if l.level > zapcore.ErrorLevel {
return
}
if err != nil {
keysAndValues = append(keysAndValues, "error", err.Error())
}
Expand Down

0 comments on commit f7b0b24

Please sign in to comment.