feat: 完善后端脚手架基础能力

This commit is contained in:
2026-08-09 01:06:21 +08:00
parent e311f416f2
commit f95311a107
61 changed files with 5630 additions and 521 deletions
+22 -1
View File
@@ -93,7 +93,19 @@ func InitLogger(cfg *config.LoggerConfig) error {
// GinLogger 返回gin的日志中间件
func GinLogger() gin.HandlerFunc {
return gin.LoggerWithWriter(os.Stdout)
return func(c *gin.Context) {
started := time.Now()
c.Next()
requestID, _ := c.Get("request_id")
Logger.Info("HTTP请求",
zap.String("request_id", stringValue(requestID)),
zap.String("method", c.Request.Method),
zap.String("path", c.Request.URL.Path),
zap.Int("status_code", c.Writer.Status()),
zap.Duration("latency", time.Since(started)),
zap.String("client_ip", c.ClientIP()),
)
}
}
// GinRecovery 返回gin的恢复中间件
@@ -108,7 +120,9 @@ func ErrorLogging() gin.HandlerFunc {
// 记录错误状态码的响应
if c.Writer.Status() >= 400 {
requestID, _ := c.Get("request_id")
Logger.Warn("HTTP错误响应",
zap.String("request_id", stringValue(requestID)),
zap.String("method", c.Request.Method),
zap.String("path", c.Request.URL.Path),
zap.String("client_ip", c.ClientIP()),
@@ -119,6 +133,13 @@ func ErrorLogging() gin.HandlerFunc {
}
}
func stringValue(value interface{}) string {
if text, ok := value.(string); ok {
return text
}
return ""
}
// Sync 同步日志缓冲区
func Sync() {
if Logger != nil {