feat(backend): replace JSON errors with user-friendly HTML error pages and reduce log noise

This commit is contained in:
pycook
2025-08-03 21:39:52 +08:00
parent 569db05b4b
commit 7c1b4b70c4
4 changed files with 199 additions and 22 deletions

View File

@@ -16,12 +16,24 @@ func LoggerMiddleware() gin.HandlerFunc {
ctx.Next()
cost := time.Since(start)
logger.L().Info(ctx.Request.URL.String(),
zap.String("method", ctx.Request.Method),
zap.Int("status", ctx.Writer.Status()),
zap.String("ip", ctx.ClientIP()),
zap.Duration("cost", cost),
)
// Only log errors and slow requests
status := ctx.Writer.Status()
if status >= 400 || cost > 1*time.Second {
logger.L().Info(ctx.Request.URL.String(),
zap.String("method", ctx.Request.Method),
zap.Int("status", status),
zap.String("ip", ctx.ClientIP()),
zap.Duration("cost", cost),
)
} else {
// Normal requests use debug level to reduce log noise
logger.L().Debug(ctx.Request.URL.String(),
zap.String("method", ctx.Request.Method),
zap.Int("status", status),
zap.String("ip", ctx.ClientIP()),
zap.Duration("cost", cost),
)
}
}
}