From 111fbdcccf35a68dd7310b33cd0ec846891a98e3 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Mon, 17 Apr 2023 20:12:26 +0800 Subject: [PATCH] caller --- pkg/common/log/zap.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/common/log/zap.go b/pkg/common/log/zap.go index 37ee7b217..167573402 100644 --- a/pkg/common/log/zap.go +++ b/pkg/common/log/zap.go @@ -102,6 +102,7 @@ func (l *ZapLogger) cores(logLevel int, isStdout bool, isJson bool, logLocation fileEncoder = zapcore.NewJSONEncoder(c) } else { c.EncodeLevel = zapcore.CapitalColorLevelEncoder + c.EncodeCaller = customCallerEncoder fileEncoder = zapcore.NewConsoleEncoder(c) } fileEncoder.AddInt("PID", os.Getpid()) @@ -126,6 +127,14 @@ func (l *ZapLogger) cores(logLevel int, isStdout bool, isJson bool, logLocation }), nil } +func customCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString("\x1b[32m") + enc.AppendString(caller.Function) + enc.AppendString(":") + enc.AppendInt(int64(caller.Line)) + enc.AppendString("\x1b[0m") +} + func (l *ZapLogger) getWriter(logLocation string, rorateCount uint) (zapcore.WriteSyncer, error) { logf, err := rotatelogs.New(logLocation+sp+"OpenIM.log.all"+".%Y-%m-%d", rotatelogs.WithRotationCount(rorateCount),