优化 Webhook 列表, 优化日志系统
This commit is contained in:
@@ -11,7 +11,7 @@ import { ensureFulfillmentCatalogBootstrapped } from './services/bootstrap/fulfi
|
||||
import { closeLocalOcrWorker, warmupLocalOcrWorker } from './services/session/ocr.js'
|
||||
import { closeAllTencentBrowserSessions, warmupTencentBrowser } from './services/session/session.js'
|
||||
import { buildSuccessPayload } from './utils/http.js'
|
||||
import { logError, logInfo, logWarn } from './utils/logger.js'
|
||||
import { createRequestId, logError, logInfo, logWarn } from './utils/logger.js'
|
||||
import tencentRouter from './routes/tencent.js'
|
||||
|
||||
const CORE_BOOT_RETRY_DELAY_MS = 5_000
|
||||
@@ -21,6 +21,38 @@ const port = Number(runtimeConfig.server.port || 3000)
|
||||
const host = '0.0.0.0'
|
||||
const startupState = createStartupState()
|
||||
|
||||
app.use((req, res, next) => {
|
||||
const startedAt = Date.now()
|
||||
const requestId = resolveRequestId(req)
|
||||
|
||||
req.requestId = requestId
|
||||
res.setHeader('X-Request-Id', requestId)
|
||||
|
||||
res.on('finish', () => {
|
||||
if (shouldSkipAccessLog(req.originalUrl, res.statusCode)) {
|
||||
return
|
||||
}
|
||||
|
||||
logInfo('[http/access]', 'request completed', {
|
||||
requestId,
|
||||
method: req.method,
|
||||
originalUrl: req.originalUrl,
|
||||
statusCode: res.statusCode,
|
||||
durationMs: Date.now() - startedAt,
|
||||
ip: req.ip,
|
||||
forwardedFor: String(req.headers['x-forwarded-for'] || ''),
|
||||
userAgent: String(req.headers['user-agent'] || ''),
|
||||
referer: String(req.headers.referer || ''),
|
||||
contentLength: Number(res.getHeader('content-length') || 0),
|
||||
actorUserId: req.adminSession?.userId || '',
|
||||
actorUsername: req.adminSession?.username || '',
|
||||
actorRole: req.adminSession?.role || '',
|
||||
})
|
||||
})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
app.use((req, res, next) => {
|
||||
res.setHeader('Access-Control-Allow-Origin', '*')
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
||||
@@ -303,6 +335,16 @@ function sleep(ms) {
|
||||
})
|
||||
}
|
||||
|
||||
function resolveRequestId(req) {
|
||||
const fromHeader = String(req.headers['x-request-id'] || '').trim()
|
||||
return fromHeader || createRequestId('req')
|
||||
}
|
||||
|
||||
function shouldSkipAccessLog(originalUrl, statusCode) {
|
||||
const pathname = String(originalUrl || '').split('?')[0]
|
||||
return ['/health', '/health/live', '/health/ready'].includes(pathname) && Number(statusCode) < 400
|
||||
}
|
||||
|
||||
let shutdownStarted = false
|
||||
|
||||
async function shutdown(signal) {
|
||||
|
||||
Reference in New Issue
Block a user