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

perf(zap): check level first #53

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 5 additions & 15 deletions zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ 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{}) {
zLevel := hLevelToZapLevel(level)
if !l.config.coreConfigs[0].Lvl.Enabled(zLevel) {
return
}
zapLogger := l.l
if len(l.config.extraKeys) > 0 {
for _, k := range l.config.extraKeys {
Expand Down Expand Up @@ -222,21 +226,7 @@ func (l *Logger) CtxFatalf(ctx context.Context, format string, v ...interface{})
}

func (l *Logger) SetLevel(level hlog.Level) {
var lvl zapcore.Level
switch level {
case hlog.LevelTrace, hlog.LevelDebug:
lvl = zap.DebugLevel
case hlog.LevelInfo:
lvl = zap.InfoLevel
case hlog.LevelWarn, hlog.LevelNotice:
lvl = zap.WarnLevel
case hlog.LevelError:
lvl = zap.ErrorLevel
case hlog.LevelFatal:
lvl = zap.FatalLevel
default:
lvl = zap.WarnLevel
}
lvl := hLevelToZapLevel(level)

l.config.coreConfigs[0].Lvl = lvl

Expand Down
25 changes: 25 additions & 0 deletions zap/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package zap

import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// InArray check if a string in a slice
func InArray(key ExtraKey, arr []ExtraKey) bool {
for _, k := range arr {
Expand All @@ -23,3 +29,22 @@ func InArray(key ExtraKey, arr []ExtraKey) bool {
}
return false
}

func hLevelToZapLevel(level hlog.Level) zapcore.Level {
var lvl zapcore.Level
switch level {
case hlog.LevelTrace, hlog.LevelDebug:
lvl = zap.DebugLevel
case hlog.LevelInfo:
lvl = zap.InfoLevel
case hlog.LevelWarn, hlog.LevelNotice:
lvl = zap.WarnLevel
case hlog.LevelError:
lvl = zap.ErrorLevel
case hlog.LevelFatal:
lvl = zap.FatalLevel
default:
lvl = zap.WarnLevel
}
return lvl
}
Loading