fix(logging): 修复 warn 级别下系统日志空白问题
- 新增 logger.WriteSinkEvent,支持旁路写入 sink,不受全局级别门控影响\n- 在 http.access 中间件中,当 info 被门控时补写 sink,保障 Ops 系统日志可索引\n- 增加 level=warn 场景回归测试,验证访问日志仍可入库
This commit is contained in:
@@ -58,6 +58,36 @@ func Logger() gin.HandlerFunc {
|
||||
|
||||
l := logger.FromContext(c.Request.Context()).With(fields...)
|
||||
l.Info("http request completed", zap.Time("completed_at", endTime))
|
||||
// 当全局日志级别高于 info(如 warn/error)时,access info 不会进入 zap core,
|
||||
// 这里补写一次 sink,保证 ops 系统日志仍可索引关键访问轨迹。
|
||||
if !logger.L().Core().Enabled(logger.LevelInfo) {
|
||||
sinkFields := map[string]any{
|
||||
"component": "http.access",
|
||||
"status_code": statusCode,
|
||||
"latency_ms": latency.Milliseconds(),
|
||||
"client_ip": clientIP,
|
||||
"protocol": protocol,
|
||||
"method": method,
|
||||
"path": path,
|
||||
"completed_at": endTime,
|
||||
}
|
||||
if requestID, ok := c.Request.Context().Value(ctxkey.RequestID).(string); ok && requestID != "" {
|
||||
sinkFields["request_id"] = requestID
|
||||
}
|
||||
if clientRequestID, ok := c.Request.Context().Value(ctxkey.ClientRequestID).(string); ok && clientRequestID != "" {
|
||||
sinkFields["client_request_id"] = clientRequestID
|
||||
}
|
||||
if hasAccountID && accountID > 0 {
|
||||
sinkFields["account_id"] = accountID
|
||||
}
|
||||
if platform != "" {
|
||||
sinkFields["platform"] = platform
|
||||
}
|
||||
if model != "" {
|
||||
sinkFields["model"] = model
|
||||
}
|
||||
logger.WriteSinkEvent("info", "http.access", "http request completed", sinkFields)
|
||||
}
|
||||
|
||||
if len(c.Errors) > 0 {
|
||||
l.Warn("http request contains gin errors", zap.String("errors", c.Errors.String()))
|
||||
|
||||
Reference in New Issue
Block a user