u
This commit is contained in:
1 parent
cf4291652a
commit
8408cf779f
20 files changed
+1400
-33
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"cn/oneao/base-go/logx"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/hertz-contrib/requestid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Log() app.HandlerFunc {
|
||||
return func(c context.Context, ctx *app.RequestContext) {
|
||||
reqId := requestid.Get(ctx)
|
||||
|
||||
c = context.WithValue(c, "RequestID", reqId)
|
||||
|
||||
start := time.Now() // 记录开始时间
|
||||
|
||||
// 执行后续处理
|
||||
ctx.Next(c)
|
||||
|
||||
// 计算耗时
|
||||
duration := time.Since(start).Milliseconds()
|
||||
|
||||
logx.CtxInfo(c, "请求完成",
|
||||
zap.String("IP", ctx.ClientIP()),
|
||||
zap.String("Method", string(ctx.Method())),
|
||||
zap.String("Path", string(ctx.Path())),
|
||||
zap.Int("Status", ctx.GetResponse().StatusCode()),
|
||||
zap.Int64("Duration", duration),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user