diff --git a/src/acp-cache-diff.ts b/src/acp-cache-diff.ts index d01d191dce..6fd8ac1e35 100644 --- a/src/acp-cache-diff.ts +++ b/src/acp-cache-diff.ts @@ -396,7 +396,7 @@ export function compareWires(aBody: Buffer, bBody: Buffer): WireRel { export interface UsageSample { ts: number; input: number; cached: number; } -const USAGE_LINE_RE = /^(\d{4}-\d{2}-\d{2}T[\d:.]+Z?) \[[a-z]+\] \[([^\]]+)\] (?:\[plugin\] )?\[acp-usage\](?: round \d+)? input=(\d+) cached=(\d+)/; +const USAGE_LINE_RE = /^(\d{4}-\d{2}-\d{2}T[\d:.]+(?:Z|[+-]\d{2}:\d{2})?) \[[a-z]+\] \[([^\]]+)\] (?:\[plugin\] )?\[acp-usage\](?: round \d+)? input=(\d+) cached=(\d+)/; export function parseUsageLog(file: string): Map { const bySession = new Map(); diff --git a/src/logger.ts b/src/logger.ts index 8db5894918..fae60efcc5 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -261,7 +261,7 @@ function warnReopenFailed(err: unknown): void { } if (!stderrDead) { try { - process.stderr.write(`${new Date().toISOString()} [warn] ${msg}\n`); + process.stderr.write(`${logTimestamp()} [warn] ${msg}\n`); } catch { /* stderr gone */ } } } @@ -306,6 +306,14 @@ export function configureLogger(file?: string): string | undefined { return file; } +/** Line timestamp: the system's LOCAL wall-clock time, no zone suffix + * (`2026-10-24T19:19:02.681`) — same shape as the old UTC stamp minus the `Z`, + * so log lines read exactly like `date` on this machine. */ +export function logTimestamp(d: Date = new Date()): string { + const p = (n: number, w = 2): string => String(n).padStart(w, "0"); + return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}T${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}.${p(d.getMilliseconds(), 3)}`; +} + /** Log a line to file + stderr. */ export const log: Logger = (level, msg) => { if (capture) { @@ -316,7 +324,7 @@ export const log: Logger = (level, msg) => { } } const safe = maskIpsInText(redactSecretsInText(msg)); - const ts = new Date().toISOString(); + const ts = logTimestamp(); const sess = sessionCtx.getStore(); const head = `${ts} [${level}]${sess ? ` [sess=${sess}]` : ""} [v=${VERSION}]`; // One prefix per PHYSICAL line (see header doc): multi-line payloads keep diff --git a/src/web/logs-query.ts b/src/web/logs-query.ts index b9dca0c1f7..5e72dd54e4 100644 --- a/src/web/logs-query.ts +++ b/src/web/logs-query.ts @@ -37,7 +37,9 @@ interface LogQueryResult { isMatch?: boolean[]; } -const TS_RE = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z)/; +// Bare local-time stamps (current format) and the older `Z` / offset forms all +// must parse: one log file outlives a format change. +const TS_RE = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?)/; function parseTs(line: string): number { const m = TS_RE.exec(line); diff --git a/tests/logger-local-time.test.ts b/tests/logger-local-time.test.ts new file mode 100644 index 0000000000..baac0217e2 --- /dev/null +++ b/tests/logger-local-time.test.ts @@ -0,0 +1,42 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { logTimestamp } from "../src/logger.ts"; +import { queryLogLines } from "../src/web/logs-query.ts"; +import { parseUsageLog } from "../src/acp-cache-diff.ts"; + +test("logTimestamp: system local wall clock, no zone suffix", () => { + const d = new Date(2026, 9, 24, 19, 19, 2, 681); + const ts = logTimestamp(d); + assert.match(ts, /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}$/); + // Local fields, exactly what `date` prints — not the UTC fields. + assert.equal(ts.slice(0, 19), "2026-10-24T19:19:02"); + assert.equal(ts.slice(11, 19), [d.getHours(), d.getMinutes(), d.getSeconds()].map((n) => String(n).padStart(2, "0")).join(":")); + // A bare stamp reads back as local time, so it round-trips on this machine. + assert.equal(Date.parse(ts), d.getTime()); +}); + +test("logs-query: window selection parses bare local stamps (and old Z stamps)", () => { + const zone = new Date(2026, 9, 24, 12, 0, 0, 0); + const local = Array.from({ length: 5 }, (_, i) => `${logTimestamp(new Date(zone.getTime() + i * 60_000))} [info] ${i === 1 ? "needle" : `noise-${i}`}`); + // ±90s around the minute-1 hit → rows 0,1,2 (rows are one minute apart). + assert.deepEqual(queryLogLines(local, "needle", { ctx: 0, winSec: 90, n: 100 }).lines, local.slice(0, 3)); + // Rotated-file stamps stay Z; they must still parse into the same window. + const legacy = ["2026-09-24T14:32:05.123Z [info] needle-a", "2026-09-24T14:32:40.000Z [info] noise-legacy", "2026-09-24T14:33:01.000Z [info] needle-b"]; + assert.deepEqual(queryLogLines(legacy, "needle", { ctx: 0, winSec: 30, n: 100 }).lines, legacy); +}); + +test("acp-cache-diff: usage lines with a bare local stamp still parse", () => { + const dir = mkdtempSync(join(tmpdir(), "bili-log-ts-")); + try { + const file = join(dir, "bili.log"); + const ts = logTimestamp(new Date(2026, 9, 24, 12, 0, 0, 0)); + writeFileSync(file, `${ts} [info] [ses_a] [acp-usage] input=1000 cached=900 (cache hit 90%)\n`); + const bySession = parseUsageLog(file); + assert.deepEqual(bySession.get("ses_a"), [{ ts: Date.parse(ts), input: 1000, cached: 900 }]); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +});