u
This commit is contained in:
1 parent
50fdacac07
commit
48cd564201
13 files changed
+220
-122
No files matched your search
@@ -5,12 +5,35 @@ import (
|
||||
"base-framework/pkg/router"
|
||||
"base-framework/pkg/routinex"
|
||||
"base-framework/pkg/utils/uid"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TrackTime 记录开始时间到现在时间的耗时
|
||||
func TrackTime(pre time.Time) time.Duration {
|
||||
elapsed := time.Since(pre)
|
||||
|
||||
return elapsed
|
||||
}
|
||||
|
||||
func Logger() router.HandlerFunc {
|
||||
return func(c *router.Context) {
|
||||
routinex.Set(logx.TrackID, uid.NextID())
|
||||
start := time.Now() // 记录开始时间
|
||||
|
||||
c.Next()
|
||||
// 生成 trackID 并存入 routinex
|
||||
trackID := uid.NextID()
|
||||
routinex.Set(logx.TrackID, trackID)
|
||||
|
||||
c.Next() // 执行业务逻辑
|
||||
|
||||
// 请求结束后计算耗时
|
||||
elapsed := TrackTime(start)
|
||||
|
||||
logx.Logger.Info("请求结束",
|
||||
logx.String("logType", "request"),
|
||||
logx.Uint64("trackID", trackID),
|
||||
logx.String("path", c.Request.URL.Path),
|
||||
logx.String("method", c.Request.Method),
|
||||
logx.Duration("elapsed", elapsed),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user