Files
toolbox/server/internal/middleware/logger.go
T
2026-08-29 23:34:01 +08:00

30 lines
768 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middleware
import (
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// Logger 基于 zap 的请求日志中间件
// 只记录 method/path/status/latency/request_id,**不记录请求体**(避免泄露用户输入)
func Logger(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
latency := time.Since(start)
logger.Info("HTTP",
zap.String("method", c.Request.Method),
zap.String("path", c.Request.URL.Path),
zap.Int("status", c.Writer.Status()),
zap.Int("size", c.Writer.Size()),
zap.String("remote_ip", c.ClientIP()),
zap.String("user_agent", c.Request.UserAgent()),
zap.String("request_id", c.GetString("request_id")),
zap.Duration("latency", latency),
)
}
}