From 2831064305937c0ae44ef897e6ebc9e51d34a1ea Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Mon, 19 Aug 2024 15:28:36 +0800 Subject: [PATCH] fix: update new logger logic. (#127) * fix: update new logger logic. * update log print. * update --- log/zap.go | 124 +++++++----------------------------------------- log/zap_test.go | 2 + 2 files changed, 19 insertions(+), 107 deletions(-) diff --git a/log/zap.go b/log/zap.go index dec2574..72f0ecc 100644 --- a/log/zap.go +++ b/log/zap.go @@ -62,20 +62,10 @@ func InitLoggerFromConfig( moduleVersion string, isSimplify bool, ) error { - var l Logger - var err error - if sdkType != "" && platformName != "" { - l, err = NewSDKZapLogger(loggerPrefixName, moduleName, sdkType, platformName, logLevel, isStdout, isJson, logLocation, rotateCount, rotationTime, moduleVersion, isSimplify) - if err != nil { - return err - } - } else { - l, err = NewZapLogger(loggerPrefixName, moduleName, logLevel, isStdout, isJson, logLocation, - rotateCount, rotationTime, moduleVersion, isSimplify) - if err != nil { - return err - } + l, err := NewZapLogger(loggerPrefixName, moduleName, sdkType, platformName, logLevel, isStdout, isJson, logLocation, rotateCount, rotationTime, moduleVersion, isSimplify) + if err != nil { + return err } pkgLogger = l.WithCallDepth(callDepth) @@ -149,7 +139,7 @@ type ZapLogger struct { } func NewZapLogger( - loggerPrefixName, moduleName string, + loggerPrefixName, moduleName string, sdkType, platformName string, logLevel int, isStdout bool, isJson bool, @@ -173,6 +163,8 @@ func NewZapLogger( loggerPrefixName: loggerPrefixName, rotationTime: time.Duration(rotationTime) * time.Hour, moduleVersion: moduleVersion, + sdkType: sdkType, + platformName: platformName, isSimplify: isSimplify, } opts, err := zl.cores(isStdout, isJson, logLocation, rotateCount) @@ -215,47 +207,6 @@ func NewConsoleZapLogger( return zl, nil } -func NewSDKZapLogger( - loggerPrefixName, moduleName string, sdkType, platformName string, - logLevel int, - isStdout bool, - isJson bool, - logLocation string, - rotateCount uint, - rotationTime uint, - moduleVersion string, - isSimplify bool, -) (*ZapLogger, error) { - zapConfig := zap.Config{ - Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]), - DisableStacktrace: true, - } - if isJson { - zapConfig.Encoding = "json" - } else { - zapConfig.Encoding = "console" - } - zl := &ZapLogger{level: logLevelMap[logLevel], - moduleName: moduleName, - loggerPrefixName: loggerPrefixName, - rotationTime: time.Duration(rotationTime) * time.Hour, - moduleVersion: moduleVersion, - sdkType: sdkType, - platformName: platformName, - isSimplify: isSimplify, - } - opts, err := zl.sdkCores(isStdout, isJson, logLocation, rotateCount) - if err != nil { - return nil, err - } - l, err := zapConfig.Build(opts) - if err != nil { - return nil, err - } - zl.zap = l.Sugar() - return zl, nil -} - func (l *ZapLogger) cores(isStdout bool, isJson bool, logLocation string, rotateCount uint) (zap.Option, error) { c := zap.NewProductionEncoderConfig() c.EncodeTime = l.timeEncoder @@ -324,60 +275,19 @@ func (l *ZapLogger) consoleCores(outPut *os.File, isJson bool) (zap.Option, erro }), nil } -func (l *ZapLogger) sdkCores(isStdout bool, isJson bool, logLocation string, rotateCount uint) (zap.Option, error) { - c := zap.NewProductionEncoderConfig() - c.EncodeTime = l.timeEncoder - c.EncodeDuration = zapcore.SecondsDurationEncoder - c.MessageKey = "msg" - c.LevelKey = "level" - c.TimeKey = "time" - c.CallerKey = "caller" - c.NameKey = "logger" - var fileEncoder zapcore.Encoder - if isJson { - c.EncodeLevel = zapcore.CapitalLevelEncoder - fileEncoder = zapcore.NewJSONEncoder(c) - fileEncoder.AddInt("PID", os.Getpid()) - fileEncoder.AddString("version", l.moduleVersion) +func (l *ZapLogger) customCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + if l.sdkType != "" && l.platformName != "" { + fixedLength := 50 + sdkPlatform := fmt.Sprintf("[%s/%s]", l.sdkType, l.platformName) + sdkPlatformFormatted := stringutil.FormatString(sdkPlatform, fixedLength, true) + enc.AppendString(sdkPlatformFormatted) } else { - c.EncodeLevel = l.capitalColorLevelEncoder - c.EncodeCaller = l.platformCallerEncoder - fileEncoder = zapcore.NewConsoleEncoder(c) + fixedLength := 50 + trimmedPath := caller.TrimmedPath() + trimmedPath = "[" + trimmedPath + "]" + s := stringutil.FormatString(trimmedPath, fixedLength, true) + enc.AppendString(s) } - fileEncoder = &alignEncoder{Encoder: fileEncoder} - writer, err := l.getWriter(logLocation, rotateCount) - if err != nil { - return nil, err - } - var cores []zapcore.Core - if logLocation != "" { - cores = []zapcore.Core{ - zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(l.level)), - } - } - if isStdout { - cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(l.level))) - } - return zap.WrapCore(func(c zapcore.Core) zapcore.Core { - return zapcore.NewTee(cores...) - }), nil -} - -func (l *ZapLogger) customCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - fixedLength := 50 - trimmedPath := caller.TrimmedPath() - trimmedPath = "[" + trimmedPath + "]" - s := stringutil.FormatString(trimmedPath, fixedLength, true) - enc.AppendString(s) -} - -func (l *ZapLogger) platformCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - fixedLength := 50 - - sdkPlatform := fmt.Sprintf("[%s/%s]", l.sdkType, l.platformName) - sdkPlatformFormatted := stringutil.FormatString(sdkPlatform, fixedLength, true) - - enc.AppendString(sdkPlatformFormatted) } func SDKLog(ctx context.Context, logLevel int, file string, line int, msg string, err error, keysAndValues []any) { diff --git a/log/zap_test.go b/log/zap_test.go index 2586952..14ec260 100644 --- a/log/zap_test.go +++ b/log/zap_test.go @@ -13,6 +13,7 @@ import ( func TestSDKLog(t *testing.T) { sdkType := "TestSDK" platformName := "testPlatform" + err := InitLoggerFromConfig( "testLogger", // loggerPrefixName "testModule", // moduleName @@ -37,6 +38,7 @@ func TestSDKLog(t *testing.T) { logger := zap.NewExample() defer logger.Sync() + ZDebug(context.Background(), "hello") SDKLog(context.Background(), 5, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value"}) SDKLog(context.Background(), 4, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value", "key", "key", 1}) SDKLog(context.Background(), 3, "cmd/abc.go", 666, "This is a test message", nil, []any{"key", "value"})