This commit is contained in:
yun
2026-09-14 00:14:30 +08:00
parent 316c6420a7
commit 7e1b474823
20 changed files with 1885 additions and 166 deletions
+13 -7
View File
@@ -13,19 +13,25 @@ import (
func main() {
ctx := context.Background()
log := loggerx.NewLogger(ctx, loggerx.SetToConsole())
log := loggerx.NewLogger(ctx,
loggerx.SetToConsole(),
loggerx.SetGinLog(true), // 把 gin 自身的 access log 也收进日志文件
)
// 进程退出前落盘并释放文件句柄
defer log.Close()
g := gin.Default()
g := gin.New() // 用 gin.New 而不是 gin.Default,避免 access log 与中间件重复记录
g.Use(gin.Recovery())
g.Use(middleware.SetGinTraceIdByLogger(log))
// 生成/透传 trace id,并写回响应头 X-Trace-Id
g.Use(middleware.SetGinTraceId(log))
g.Use(middleware.SetGinParams(log))
g.GET("/ping", func(ctx *gin.Context) {
log.Infof(ctx, "GET /ping")
ctx.JSON(200, gin.H{"message": "pong"})
g.GET("/ping", func(c *gin.Context) {
// trace id 通过 request context 传给日志,便于串起整条链路
log.Infof(c.Request.Context(), "GET /ping")
c.JSON(200, gin.H{"message": "pong", "trace_id": middleware.GetTraceId(c.Request.Context(), log.GetTraceField())})
})
g.Run(":8080")
_ = g.Run(":8080")
}