Skip to content

Commit

Permalink
refactor: 使用zap提供的全局logger
Browse files Browse the repository at this point in the history
  • Loading branch information
SugarMGP authored and Penryn committed Oct 25, 2024
1 parent 3caf273 commit 54b7986
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
5 changes: 2 additions & 3 deletions app/midwares/errHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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]))

Expand All @@ -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),
)
Expand Down
23 changes: 14 additions & 9 deletions app/utils/jsonResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"runtime"

"4u-go/app/apiException"
"4u-go/app/utils/log"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -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...)
}
}

Expand Down
10 changes: 4 additions & 6 deletions app/utils/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 返回日志级别
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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())
}
}

0 comments on commit 54b7986

Please sign in to comment.