Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/log: improve performance of log.Debug() #4304

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions pkg/log/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,27 @@ import (

// Debug logs at debug level.
func Debug(msg string, ctx ...interface{}) {
zap.L().Debug(msg, convertCtx(ctx)...)
if enabled(DebugLevel) {
zap.L().Debug(msg, convertCtx(ctx)...)
}
}

// Info logs at info level.
func Info(msg string, ctx ...interface{}) {
zap.L().Info(msg, convertCtx(ctx)...)
if enabled(InfoLevel) {
zap.L().Info(msg, convertCtx(ctx)...)
}
}

// Error logs at error level.
func Error(msg string, ctx ...interface{}) {
zap.L().Error(msg, convertCtx(ctx)...)
if enabled(ErrorLevel) {
zap.L().Error(msg, convertCtx(ctx)...)
}
}

func enabled(lvl Level) bool {
return zap.L().Core().Enabled(zapcore.Level(lvl))
}

// WithOptions returns the logger with the options applied.
Expand Down Expand Up @@ -72,15 +82,21 @@ func (l *logger) New(ctx ...interface{}) Logger {
}

func (l *logger) Debug(msg string, ctx ...interface{}) {
l.logger.Debug(msg, convertCtx(ctx)...)
if l.Enabled(DebugLevel) {
l.logger.Debug(msg, convertCtx(ctx)...)
}
}

func (l *logger) Info(msg string, ctx ...interface{}) {
l.logger.Info(msg, convertCtx(ctx)...)
if l.Enabled(InfoLevel) {
l.logger.Info(msg, convertCtx(ctx)...)
}
}

func (l *logger) Error(msg string, ctx ...interface{}) {
l.logger.Error(msg, convertCtx(ctx)...)
if l.Enabled(ErrorLevel) {
l.logger.Error(msg, convertCtx(ctx)...)
}
}

func (l *logger) Enabled(lvl Level) bool {
Expand Down