重构日志与可观测性体系
新增单行文本编码器与结构化 GORM 日志,统一错误记录与请求日志策略,收紧日志文件权限并修复按天切分与压缩,支付回调参数脱敏,生产强制阿里云短信,RequestID 校验防注入,日志文案中文化。
This commit is contained in:
@@ -11,22 +11,15 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Recovery(logger *zap.Logger) gin.HandlerFunc {
|
||||
const contextPanicStack = "panic_stack"
|
||||
|
||||
func Recovery(_ *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()),
|
||||
)
|
||||
c.Set(contextPanicStack, string(debug.Stack()))
|
||||
if !c.Writer.Written() {
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "服务暂时不可用")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user