u
This commit is contained in:
1 parent
3420976bd6
commit
ad47e5167b
8 files changed
+184
-70
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"allapp-go/pkg/logger"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Logger() fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
traceID := c.RequestID()
|
||||
|
||||
logger.SetTraceID(c, traceID)
|
||||
|
||||
start := time.Now()
|
||||
|
||||
err := c.Next()
|
||||
|
||||
// ✅ 计算耗时
|
||||
cost := time.Since(start)
|
||||
|
||||
log := logger.FromCtx(c.Context())
|
||||
log.Info("HTTP 请求",
|
||||
zap.String("method", c.Method()),
|
||||
zap.String("path", c.Path()),
|
||||
zap.Int("status", c.Response().StatusCode()),
|
||||
zap.String("ip", c.IP()),
|
||||
zap.String("duration", cost.String()),
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user