惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
月光博客
月光博客
IT之家
IT之家
B
Blog RSS Feed
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
博客园_首页
B
Blog
V
V2EX
腾讯CDC
Vercel News
Vercel News
量子位
Microsoft Security Blog
Microsoft Security Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(logging): rotate file logs instead of suppressing · o...
vincentkoc · 2026-04-26 · via Recent Commits to openclaw:main

@@ -55,7 +55,8 @@ export const DEFAULT_LOG_FILE = resolveDefaultLogFile(DEFAULT_LOG_DIR); // legac

5555

const LOG_PREFIX = "openclaw";

5656

const LOG_SUFFIX = ".log";

5757

const MAX_LOG_AGE_MS = 24 * 60 * 60 * 1000; // 24h

58-

const DEFAULT_MAX_LOG_FILE_BYTES = 500 * 1024 * 1024; // 500 MB

58+

const DEFAULT_MAX_LOG_FILE_BYTES = 100 * 1024 * 1024; // 100 MB

59+

const MAX_ROTATED_LOG_FILES = 5;

59606061

type LogObj = { date?: Date } & Record<string, unknown>;

6162

@@ -397,39 +398,45 @@ function buildLogger(settings: ResolvedSettings): TsLogger<LogObj> {

397398

return logger;

398399

}

399400400-

fs.mkdirSync(path.dirname(settings.file), { recursive: true });

401+

const rollingFile = isRollingPath(settings.file);

402+

let activeFile = resolveActiveLogFile(settings.file);

403+

fs.mkdirSync(path.dirname(activeFile), { recursive: true });

401404

// Clean up stale rolling logs when using a dated log filename.

402-

if (isRollingPath(settings.file)) {

403-

pruneOldRollingLogs(path.dirname(settings.file));

405+

if (rollingFile) {

406+

pruneOldRollingLogs(path.dirname(activeFile));

404407

}

405-

let currentFileBytes = getCurrentLogFileBytes(settings.file);

406-

let warnedAboutSizeCap = false;

408+

let currentFileBytes = getCurrentLogFileBytes(activeFile);

409+

let warnedAboutRotationFailure = false;

407410408411

logger.attachTransport((logObj: LogObj) => {

409412

try {

413+

const nextActiveFile = resolveActiveLogFile(settings.file);

414+

if (nextActiveFile !== activeFile) {

415+

activeFile = nextActiveFile;

416+

fs.mkdirSync(path.dirname(activeFile), { recursive: true });

417+

if (rollingFile) {

418+

pruneOldRollingLogs(path.dirname(activeFile));

419+

}

420+

currentFileBytes = getCurrentLogFileBytes(activeFile);

421+

}

410422

const time = formatTimestamp(logObj.date ?? new Date(), { style: "long" });

411423

const line = redactSensitiveText(JSON.stringify({ ...logObj, time }));

412424

const payload = `${line}\n`;

413425

const payloadBytes = Buffer.byteLength(payload, "utf8");

414426

const nextBytes = currentFileBytes + payloadBytes;

415-

if (nextBytes > settings.maxFileBytes) {

416-

if (!warnedAboutSizeCap) {

417-

warnedAboutSizeCap = true;

418-

const warningLine = JSON.stringify({

419-

time: formatTimestamp(new Date(), { style: "long" }),

420-

level: "warn",

421-

subsystem: "logging",

422-

message: `log file size cap reached; suppressing writes file=${settings.file} maxFileBytes=${settings.maxFileBytes}`,

423-

});

424-

appendLogLine(settings.file, `${warningLine}\n`);

427+

if (currentFileBytes > 0 && nextBytes > settings.maxFileBytes) {

428+

if (rotateLogFile(activeFile)) {

429+

currentFileBytes = getCurrentLogFileBytes(activeFile);

430+

warnedAboutRotationFailure = false;

431+

} else if (!warnedAboutRotationFailure) {

432+

warnedAboutRotationFailure = true;

425433

process.stderr.write(

426-

`[openclaw] log file size cap reached; suppressing writes file=${settings.file} maxFileBytes=${settings.maxFileBytes}\n`,

434+

`[openclaw] log file rotation failed; continuing writes file=${activeFile} maxFileBytes=${settings.maxFileBytes}\n`,

427435

);

428436

}

429-

return;

430437

}

431-

if (appendLogLine(settings.file, payload)) {

432-

currentFileBytes = nextBytes;

438+

if (appendLogLine(activeFile, payload)) {

439+

currentFileBytes += payloadBytes;

433440

}

434441

} catch {

435442

// never block on logging failures

@@ -554,8 +561,19 @@ function formatLocalDate(date: Date): string {

554561

}

555562556563

function defaultRollingPathForToday(): string {

557-

const today = formatLocalDate(new Date());

558-

return path.join(DEFAULT_LOG_DIR, `${LOG_PREFIX}-${today}${LOG_SUFFIX}`);

564+

return rollingPathForDate(DEFAULT_LOG_DIR, new Date());

565+

}

566+567+

function rollingPathForDate(dir: string, date: Date): string {

568+

const today = formatLocalDate(date);

569+

return path.join(dir, `${LOG_PREFIX}-${today}${LOG_SUFFIX}`);

570+

}

571+572+

function resolveActiveLogFile(file: string): string {

573+

if (!isRollingPath(file)) {

574+

return file;

575+

}

576+

return rollingPathForDate(path.dirname(file), new Date());

559577

}

560578561579

function isRollingPath(file: string): boolean {

@@ -592,3 +610,29 @@ function pruneOldRollingLogs(dir: string): void {

592610

// ignore missing dir or read errors

593611

}

594612

}

613+614+

function rotatedLogPath(file: string, index: number): string {

615+

const ext = path.extname(file);

616+

const base = file.slice(0, file.length - ext.length);

617+

return `${base}.${index}${ext}`;

618+

}

619+620+

function rotateLogFile(file: string): boolean {

621+

try {

622+

fs.mkdirSync(path.dirname(file), { recursive: true });

623+

fs.rmSync(rotatedLogPath(file, MAX_ROTATED_LOG_FILES), { force: true });

624+

for (let index = MAX_ROTATED_LOG_FILES - 1; index >= 1; index -= 1) {

625+

const from = rotatedLogPath(file, index);

626+

if (!fs.existsSync(from)) {

627+

continue;

628+

}

629+

fs.renameSync(from, rotatedLogPath(file, index + 1));

630+

}

631+

if (fs.existsSync(file)) {

632+

fs.renameSync(file, rotatedLogPath(file, 1));

633+

}

634+

return true;

635+

} catch {

636+

return false;

637+

}

638+

}