chore: improve log print

This commit is contained in:
ryan
2026-07-13 16:10:26 +08:00
parent 6b6c786cfe
commit a61d55bb1b
3 changed files with 82 additions and 87 deletions
+24 -12
View File
@@ -50,18 +50,7 @@ func loggerMiddleware() gin.HandlerFunc {
// 排除健康检查接口
healthPath := config.Config.App.APIPrefix + "/health"
if c.Request.URL.Path != healthPath {
logger.InfoF(
ctx,
"[LoggerMiddleware] %s %s\nStartTime: %s\nEndTime: %s\nLatency: %d\nClientIP: %s\nResponse: %d %d",
c.Request.Method,
path,
start.Format(time.RFC3339),
end.Format(time.RFC3339),
latency.Milliseconds(),
c.ClientIP(),
c.Writer.Status(),
c.Writer.Size(),
)
logRequest(ctx, c, path, start, end, latency)
}
// 设置 Span 状态
@@ -72,6 +61,29 @@ func loggerMiddleware() gin.HandlerFunc {
}
}
func logRequest(ctx context.Context, c *gin.Context, path string, start, end time.Time, latency time.Duration) {
format := "[LoggerMiddleware] %s %s\nStartTime: %s\nEndTime: %s\nLatency: %d\nClientIP: %s\nResponse: %d %d"
args := []interface{}{
c.Request.Method,
path,
start.Format(time.RFC3339),
end.Format(time.RFC3339),
latency.Milliseconds(),
c.ClientIP(),
c.Writer.Status(),
c.Writer.Size(),
}
switch {
case c.Writer.Status() >= http.StatusInternalServerError:
logger.ErrorF(ctx, format, args...)
case c.Writer.Status() >= http.StatusBadRequest:
logger.WarnF(ctx, format, args...)
default:
logger.DebugF(ctx, format, args...)
}
}
func isOriginAllowed(ctx context.Context, origin string) bool {
sc, err := repository.GetSystemConfigByKey(ctx, model.ConfigKeyServerAddress)
if err != nil || sc.Value == "" {