对接日志默认仅记录告警与错误

新增 LOG_INTEGRATION_LEVEL(默认 warn),压低 integration-*.log 体积;
失败类文案自动提升为 error,避免异常被过滤。
This commit is contained in:
yml2213
2026-07-10 21:17:55 +08:00
parent 50d32be0ea
commit 01f46e4fc7
9 changed files with 43 additions and 2 deletions
+7
View File
@@ -26,6 +26,13 @@ test('shouldWriteLog respects configured minimum log level', () => {
assert.equal(shouldWriteLog('error', 'warn'), true)
})
test('shouldWriteLog at warn filters info-style integration noise', () => {
// 与默认 LOG_INTEGRATION_LEVEL=warn 行为一致
assert.equal(shouldWriteLog('info', 'warn'), false)
assert.equal(shouldWriteLog('warn', 'warn'), true)
assert.equal(shouldWriteLog('error', 'warn'), true)
})
test('resolveLogFilePath uses daily log file names by channel', () => {
assert.match(resolveLogFilePath('app', '2026-05-28T00:00:19.194+08:00'), /app-2026-05-28\.log$/)
assert.match(
+25 -2
View File
@@ -50,6 +50,10 @@ const ANSI = {
const DATA_ROOT = resolveDataRoot()
const LOG_DIR = path.join(DATA_ROOT, 'logs')
const ACTIVE_LOG_LEVEL = normalizeLogLevel(runtimeConfig.logging?.level)
/** integration 通道独立阈值,默认 warn(仅错误/告警落盘) */
const ACTIVE_INTEGRATION_LOG_LEVEL = normalizeLogLevel(
runtimeConfig.logging?.integrationLevel ?? 'warn',
)
const DEFAULT_LOG_RETENTION_DAYS = 30
const LOG_RETENTION_DAYS = normalizeLogRetentionDays(runtimeConfig.logging?.retentionDays)
const LEGACY_LOG_FILES = new Set(['app.log', 'integration.log'])
@@ -90,7 +94,24 @@ export function logIntegration(
detail: LogDetail = undefined,
{ level = 'info' }: { level?: LogLevel } = {},
) {
return writeLog(level, scope, message, detail, { channel: 'integration' })
// 默认 integration 阈值为 warn:失败类文案即使调用方传 info 也提升,避免漏记异常
return writeLog(resolveIntegrationLogLevel(level, message), scope, message, detail, {
channel: 'integration',
})
}
function resolveIntegrationLogLevel(level: LogLevel, message: unknown): LogLevel {
const normalized = normalizeLogLevel(level)
if (normalized === 'warn' || normalized === 'error') {
return normalized
}
const text = String(message || '')
if (/失败|异常|错误|超时|拒绝|expired|fail|error|timeout|denied/i.test(text)) {
return 'error'
}
return normalized
}
export function logExternalHttpPacket(
@@ -109,7 +130,9 @@ function writeLog(
detail: LogDetail,
{ channel = 'app' }: { channel?: LogChannel } = {},
): LogEntry | null {
if (!shouldWriteLog(level, ACTIVE_LOG_LEVEL)) {
const configuredLevel =
channel === 'integration' ? ACTIVE_INTEGRATION_LOG_LEVEL : ACTIVE_LOG_LEVEL
if (!shouldWriteLog(level, configuredLevel)) {
return null
}