From 01f46e4fc7f9e6123bc735dc90ddc1a09f34a78a Mon Sep 17 00:00:00 2001 From: yml2213 Date: Fri, 10 Jul 2026 21:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E6=97=A5=E5=BF=97=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=BB=85=E8=AE=B0=E5=BD=95=E5=91=8A=E8=AD=A6=E4=B8=8E?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 LOG_INTEGRATION_LEVEL(默认 warn),压低 integration-*.log 体积; 失败类文案自动提升为 error,避免异常被过滤。 --- .env.mac-docker.example | 2 ++ .env.server.example | 2 ++ apps/backend/src/config/defaults.ts | 2 ++ apps/backend/src/config/env-overrides.test.ts | 1 + apps/backend/src/config/env-overrides.ts | 1 + .../src/config/runtime-validation.test.ts | 1 + apps/backend/src/types/runtime-config.ts | 2 ++ apps/backend/src/utils/logger.test.ts | 7 +++++ apps/backend/src/utils/logger.ts | 27 +++++++++++++++++-- 9 files changed, 43 insertions(+), 2 deletions(-) diff --git a/.env.mac-docker.example b/.env.mac-docker.example index b6fd3cc1..a67833b7 100644 --- a/.env.mac-docker.example +++ b/.env.mac-docker.example @@ -19,6 +19,8 @@ POSTGRES_PORT=5432 # Logging LOG_LEVEL=info +# integration-*.log 默认 warn,只记对接异常;排查全量时改为 info +LOG_INTEGRATION_LEVEL=warn LOG_RETENTION_DAYS=30 # Admin diff --git a/.env.server.example b/.env.server.example index 935bef6d..9e965a07 100644 --- a/.env.server.example +++ b/.env.server.example @@ -18,6 +18,8 @@ DATABASE_URL=postgres://postgres:change-me-postgres-password@postgres:5432/order # Logging LOG_LEVEL=info +# integration-*.log:对接日志级别(debug|info|warn|error),默认 warn 只记异常 +LOG_INTEGRATION_LEVEL=warn LOG_RETENTION_DAYS=30 # Admin diff --git a/apps/backend/src/config/defaults.ts b/apps/backend/src/config/defaults.ts index f9c1044d..1c4931fa 100644 --- a/apps/backend/src/config/defaults.ts +++ b/apps/backend/src/config/defaults.ts @@ -14,6 +14,8 @@ export function createDefaultRuntimeConfig(projectRoot: string): RuntimeConfig { logging: { level: "info", + // 对接日志默认只保留 warn/error,避免 integration-*.log 被 info 刷爆 + integrationLevel: "warn", retentionDays: 30, }, diff --git a/apps/backend/src/config/env-overrides.test.ts b/apps/backend/src/config/env-overrides.test.ts index eb1fc7d2..133958d2 100644 --- a/apps/backend/src/config/env-overrides.test.ts +++ b/apps/backend/src/config/env-overrides.test.ts @@ -82,6 +82,7 @@ function createRuntimeConfig(): RuntimeConfig { }, logging: { level: 'info', + integrationLevel: 'warn', retentionDays: 30, }, database: { diff --git a/apps/backend/src/config/env-overrides.ts b/apps/backend/src/config/env-overrides.ts index 0628d034..207633f5 100644 --- a/apps/backend/src/config/env-overrides.ts +++ b/apps/backend/src/config/env-overrides.ts @@ -33,6 +33,7 @@ export const ENV_OVERRIDES: readonly EnvOverride[] = [ integerEnv("PORT", ["server", "port"]), stringEnv("DATA_ROOT", ["data", "root"], path.resolve), stringEnv("LOG_LEVEL", ["logging", "level"]), + stringEnv("LOG_INTEGRATION_LEVEL", ["logging", "integrationLevel"]), integerEnv("LOG_RETENTION_DAYS", ["logging", "retentionDays"]), stringEnv("DATABASE_URL", ["database", "url"]), booleanEnv("DATABASE_SSL", ["database", "ssl"]), diff --git a/apps/backend/src/config/runtime-validation.test.ts b/apps/backend/src/config/runtime-validation.test.ts index e59d47e9..e8acb952 100644 --- a/apps/backend/src/config/runtime-validation.test.ts +++ b/apps/backend/src/config/runtime-validation.test.ts @@ -117,6 +117,7 @@ function createRuntimeConfig(overrides: Record = {}): RuntimeCo }, logging: { level: 'info', + integrationLevel: 'warn', }, database: { url: 'postgres://postgres:postgres@postgres:5432/order_site', diff --git a/apps/backend/src/types/runtime-config.ts b/apps/backend/src/types/runtime-config.ts index 0efd5ecc..d27686b4 100644 --- a/apps/backend/src/types/runtime-config.ts +++ b/apps/backend/src/types/runtime-config.ts @@ -24,6 +24,8 @@ export type RuntimeConfig = { }; logging: { level: string; + /** integration 通道(外部对接日志文件)最低级别,默认 warn */ + integrationLevel: string; retentionDays: number; }; database: { diff --git a/apps/backend/src/utils/logger.test.ts b/apps/backend/src/utils/logger.test.ts index 0f98d2de..50af10fb 100644 --- a/apps/backend/src/utils/logger.test.ts +++ b/apps/backend/src/utils/logger.test.ts @@ -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( diff --git a/apps/backend/src/utils/logger.ts b/apps/backend/src/utils/logger.ts index 86f1c55d..7026f16f 100644 --- a/apps/backend/src/utils/logger.ts +++ b/apps/backend/src/utils/logger.ts @@ -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 }