From 54b7986184aa3063542ceb7ec0e4d9b7d8d7b1f1 Mon Sep 17 00:00:00 2001 From: SugarMGP <2350745751@qq.com> Date: Sat, 26 Oct 2024 00:33:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8zap=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E7=9A=84=E5=85=A8=E5=B1=80logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/midwares/errHandler.go | 5 ++--- app/utils/jsonResponse.go | 23 ++++++++++++++--------- app/utils/log/log.go | 10 ++++------ main.go | 9 +++++---- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/app/midwares/errHandler.go b/app/midwares/errHandler.go index 84d0c46..33e714d 100644 --- a/app/midwares/errHandler.go +++ b/app/midwares/errHandler.go @@ -6,7 +6,6 @@ import ( "4u-go/app/apiException" "4u-go/app/utils" - "4u-go/app/utils/log" "github.com/gin-gonic/gin" "go.uber.org/zap" ) @@ -22,7 +21,7 @@ func ErrHandler() gin.HandlerFunc { stackTrace := make([]byte, 4096) stackSize := runtime.Stack(stackTrace, true) // After - log.Logger.Panic("Panic recovered", + zap.L().Panic("Panic recovered", zap.Any("error", err), zap.ByteString("stackTrace", stackTrace[:stackSize])) @@ -40,7 +39,7 @@ func ErrHandler() gin.HandlerFunc { func HandleNotFound(c *gin.Context) { err := apiException.NotFound // 记录 404 错误日志 - log.Logger.Warn("404 Not Found", + zap.L().Warn("404 Not Found", zap.String("path", c.Request.URL.Path), zap.String("method", c.Request.Method), ) diff --git a/app/utils/jsonResponse.go b/app/utils/jsonResponse.go index c78664f..5b527b6 100644 --- a/app/utils/jsonResponse.go +++ b/app/utils/jsonResponse.go @@ -5,7 +5,6 @@ import ( "runtime" "4u-go/app/apiException" - "4u-go/app/utils/log" "github.com/gin-gonic/gin" "go.uber.org/zap" ) @@ -58,14 +57,20 @@ func logError(c *gin.Context, apiErr *apiException.Error, level Level, err error zap.Int("line", line), // 记录代码行号 zap.Error(err), // 记录原始错误信息 } - // 记录日志 - switch level { - case LevelError: - log.Logger.Error(apiErr.Msg, logFields...) - case LevelWarn: - log.Logger.Warn(apiErr.Msg, logFields...) - case LevelInfo: - log.Logger.Info(apiErr.Msg, logFields...) + // 创建日志级别映射表 + logMap := map[Level]func(string, ...zap.Field){ + LevelFatal: zap.L().Fatal, + LevelPanic: zap.L().Panic, + LevelDpanic: zap.L().DPanic, + LevelError: zap.L().Error, + LevelWarn: zap.L().Warn, + LevelInfo: zap.L().Info, + LevelDebug: zap.L().Debug, + } + + // 根据日志级别记录日志 + if logFunc, ok := logMap[level]; ok { + logFunc(apiErr.Msg, logFields...) } } diff --git a/app/utils/log/log.go b/app/utils/log/log.go index 17ccb63..eeaef74 100644 --- a/app/utils/log/log.go +++ b/app/utils/log/log.go @@ -6,15 +6,11 @@ import ( "strings" "4u-go/config/config" - "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/natefinch/lumberjack.v2" ) -// Logger 是应用程序的全局日志记录器 -var Logger *zap.Logger - // Dir 存储日志文件目录 var Dir string @@ -98,8 +94,10 @@ func ZapInit() { // 添加其他选项 addAdditionalOptions(cfg, &options) - Logger = zap.New(combinedCore, options...) // 创建新的 zap 日志记录器 - Logger.Info("Logger initialized") // 初始化日志记录器信息 + logger := zap.New(combinedCore, options...) // 创建新的 zap 日志记录器 + zap.ReplaceGlobals(logger) // 替换全局日志记录器 + + zap.L().Info("Logger initialized") // 初始化日志记录器信息 } // getLoggerLevel 返回日志级别 diff --git a/main.go b/main.go index 361987d..bc6618f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "4u-go/config/wechat" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" + "go.uber.org/zap" ) func main() { @@ -19,18 +20,18 @@ func main() { r.NoRoute(midwares.HandleNotFound) log.ZapInit() if err := database.Init(); err != nil { - log.Logger.Fatal(err.Error()) // 在 main 函数中处理错误并终止程序 + zap.L().Fatal(err.Error()) // 在 main 函数中处理错误并终止程序 } if err := session.Init(r); err != nil { - log.Logger.Fatal(err.Error()) + zap.L().Fatal(err.Error()) } if err := wechat.Init(); err != nil { - log.Logger.Fatal(err.Error()) + zap.L().Fatal(err.Error()) } router.Init(r) err := r.Run() if err != nil { - log.Logger.Fatal(err.Error()) + zap.L().Fatal(err.Error()) } }