优化:降低访问日志噪声并增加日志文件轮转

This commit is contained in:
yml2213
2026-08-30 16:59:11 +08:00
parent 6ca54e468e
commit 25b89bb503
20 changed files with 2334 additions and 4 deletions
+16 -2
View File
@@ -1,5 +1,5 @@
import type { Request, Response, NextFunction } from 'express'
import { createRequestId, logInfo } from '../utils/logger.js'
import { createRequestId, logDebug, logInfo } from '../utils/logger.js'
export function accessLogMiddleware(req: Request, res: Response, next: NextFunction): void {
const startedAt = Date.now()
@@ -13,7 +13,9 @@ export function accessLogMiddleware(req: Request, res: Response, next: NextFunct
return
}
logInfo('[http/access]', 'request completed', {
const writeLog =
resolveAccessLogLevel(req.method, res.statusCode) === 'debug' ? logDebug : logInfo
writeLog('[http/access]', 'request completed', {
requestId,
method: req.method,
originalUrl: req.originalUrl,
@@ -33,6 +35,18 @@ export function accessLogMiddleware(req: Request, res: Response, next: NextFunct
next()
}
export function resolveAccessLogLevel(method: unknown, statusCode: unknown): 'debug' | 'info' {
const normalizedMethod = String(method || '')
.trim()
.toUpperCase()
const normalizedStatus = Number(statusCode || 0)
return ['GET', 'HEAD'].includes(normalizedMethod) &&
normalizedStatus >= 200 &&
normalizedStatus < 400
? 'debug'
: 'info'
}
function resolveRequestId(req: Request): string {
const fromHeader = String(req.headers['x-request-id'] || '').trim()
return fromHeader || createRequestId('req')