完善日志和审计链路

This commit is contained in:
yml
2026-06-02 21:40:23 +08:00
parent 1325c8fb89
commit 6dcea2d56c
20 changed files with 243 additions and 90 deletions
+38
View File
@@ -0,0 +1,38 @@
package middleware
import (
"fmt"
"net/http"
"runtime/debug"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func Recovery(logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if recovered := recover(); recovered != nil {
err := fmt.Errorf("panic: %v", recovered)
_ = c.Error(err)
logger.Error("http panic recovered",
zap.String("request_id", GetRequestID(c)),
zap.String("method", c.Request.Method),
zap.String("path", c.Request.URL.Path),
zap.String("route", c.FullPath()),
zap.String("client_ip", c.ClientIP()),
zap.String("user_agent", c.GetHeader("User-Agent")),
zap.String("panic", fmt.Sprint(recovered)),
zap.ByteString("stack", debug.Stack()),
)
if !c.Writer.Written() {
response.Error(c, http.StatusInternalServerError, "internal_error", "服务暂时不可用")
}
c.Abort()
}
}()
c.Next()
}
}