This commit is contained in:
wangchuxiao 2023-04-18 10:45:23 +08:00
parent fb668b3f0c
commit f4020a2508
2 changed files with 7 additions and 12 deletions

View File

@ -20,7 +20,7 @@ const (
var ( var (
_levelToColor = map[zapcore.Level]Color{ _levelToColor = map[zapcore.Level]Color{
zapcore.DebugLevel: Magenta, zapcore.DebugLevel: White,
zapcore.InfoLevel: Blue, zapcore.InfoLevel: Blue,
zapcore.WarnLevel: Yellow, zapcore.WarnLevel: Yellow,
zapcore.ErrorLevel: Red, zapcore.ErrorLevel: Red,

View File

@ -60,8 +60,6 @@ func ZError(ctx context.Context, msg string, err error, keysAndValues ...interfa
type ZapLogger struct { type ZapLogger struct {
zap *zap.SugaredLogger zap *zap.SugaredLogger
level zapcore.Level level zapcore.Level
callerKey string
loggerKey string
} }
func NewZapLogger(logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) (*ZapLogger, error) { func NewZapLogger(logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) (*ZapLogger, error) {
@ -99,10 +97,6 @@ func (l *ZapLogger) cores(isStdout bool, isJson bool, logLocation string, rotate
c.MessageKey = "msg" c.MessageKey = "msg"
c.LevelKey = "level" c.LevelKey = "level"
c.TimeKey = "time" c.TimeKey = "time"
l.callerKey = "caller"
l.loggerKey = "logger"
c.CallerKey = l.callerKey
l.loggerKey = c.NameKey
var fileEncoder zapcore.Encoder var fileEncoder zapcore.Encoder
if isJson { if isJson {
@ -112,13 +106,12 @@ func (l *ZapLogger) cores(isStdout bool, isJson bool, logLocation string, rotate
c.EncodeLevel = l.CapitalColorLevelEncoder c.EncodeLevel = l.CapitalColorLevelEncoder
customCallerEncoder := func(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { customCallerEncoder := func(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
s := "[" + caller.TrimmedPath() + "]" s := "[" + caller.TrimmedPath() + "]"
pid := fmt.Sprintf("["+"PID:"+"%d"+"]", os.Getpid())
color, ok := _levelToColor[l.level] color, ok := _levelToColor[l.level]
if !ok { if !ok {
color = _levelToColor[zapcore.ErrorLevel] color = _levelToColor[zapcore.ErrorLevel]
} }
enc.AppendString(color.Add(s)) enc.AppendString(color.Add(s))
enc.AppendString(color.Add(pid))
} }
c.EncodeCaller = customCallerEncoder c.EncodeCaller = customCallerEncoder
fileEncoder = zapcore.NewConsoleEncoder(c) fileEncoder = zapcore.NewConsoleEncoder(c)
@ -161,7 +154,9 @@ func (l *ZapLogger) CapitalColorLevelEncoder(level zapcore.Level, enc zapcore.Pr
if !ok { if !ok {
s = _unknownLevelColor[zapcore.ErrorLevel] s = _unknownLevelColor[zapcore.ErrorLevel]
} }
pid := fmt.Sprintf("["+"PID:"+"%d"+"]", os.Getpid())
color := _levelToColor[level]
enc.AppendString(color.Add(pid))
enc.AppendString(s) enc.AppendString(s)
} }