u
This commit is contained in:
1 parent
755fc20c9a
commit
a57070f860
9 files changed
+557
-93
No files matched your search
@@ -13,7 +13,7 @@ func Error() app.HandlerFunc {
|
||||
|
||||
if err := c.Errors.Last(); err != nil {
|
||||
// 未知错误 -> 500
|
||||
c.JSON(500, ErrorResponse{500, "Internal Server Error"})
|
||||
//c.JSON(500, ErrorResponse{500, "Internal Server Error"})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"github.com/hertz-contrib/requestid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Log() app.HandlerFunc {
|
||||
return func(c context.Context, ctx *app.RequestContext) {
|
||||
start := time.Now() // 记录开始时间
|
||||
|
||||
// 执行后续处理
|
||||
ctx.Next(c)
|
||||
|
||||
// 计算耗时
|
||||
duration := time.Since(start).Milliseconds()
|
||||
|
||||
hlog.Info("请求完成",
|
||||
zap.String("RequestID", requestid.Get(ctx)),
|
||||
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