fix: align rolling logs to local time

This commit is contained in:
Peter Steinberger
2026-01-22 06:53:56 +00:00
parent b91e72824f
commit d19a0249f8
7 changed files with 103 additions and 3 deletions

View File

@@ -0,0 +1,51 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import { resetLogger, setLoggerOverride } from "../../logging.js";
import { logsHandlers } from "./logs.js";
const noop = () => false;
describe("logs.tail", () => {
afterEach(() => {
resetLogger();
setLoggerOverride(null);
});
it("falls back to latest rolling log file when today is missing", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-logs-"));
const older = path.join(tempDir, "clawdbot-2026-01-20.log");
const newer = path.join(tempDir, "clawdbot-2026-01-21.log");
await fs.writeFile(older, '{"msg":"old"}\n');
await fs.writeFile(newer, '{"msg":"new"}\n');
await fs.utimes(older, new Date(0), new Date(0));
await fs.utimes(newer, new Date(), new Date());
setLoggerOverride({ file: path.join(tempDir, "clawdbot-2026-01-22.log") });
const respond = vi.fn();
await logsHandlers["logs.tail"]({
params: {},
respond,
context: {} as unknown as Parameters<(typeof logsHandlers)["logs.tail"]>[0]["context"],
client: null,
req: { id: "req-1", type: "req", method: "logs.tail" },
isWebchatConnect: noop,
});
expect(respond).toHaveBeenCalledWith(
true,
expect.objectContaining({
file: newer,
lines: ['{"msg":"new"}'],
}),
undefined,
);
await fs.rm(tempDir, { recursive: true, force: true });
});
});

View File

@@ -1,4 +1,5 @@
import fs from "node:fs/promises";
import path from "node:path";
import { getResolvedLoggerSettings } from "../../logging.js";
import {
ErrorCodes,
@@ -12,11 +13,40 @@ const DEFAULT_LIMIT = 500;
const DEFAULT_MAX_BYTES = 250_000;
const MAX_LIMIT = 5000;
const MAX_BYTES = 1_000_000;
const ROLLING_LOG_RE = /^clawdbot-\d{4}-\d{2}-\d{2}\.log$/;
function clamp(value: number, min: number, max: number) {
return Math.max(min, Math.min(max, value));
}
function isRollingLogFile(file: string): boolean {
return ROLLING_LOG_RE.test(path.basename(file));
}
async function resolveLogFile(file: string): Promise<string> {
const stat = await fs.stat(file).catch(() => null);
if (stat) return file;
if (!isRollingLogFile(file)) return file;
const dir = path.dirname(file);
const entries = await fs.readdir(dir, { withFileTypes: true }).catch(() => null);
if (!entries) return file;
const candidates = await Promise.all(
entries
.filter((entry) => entry.isFile() && ROLLING_LOG_RE.test(entry.name))
.map(async (entry) => {
const fullPath = path.join(dir, entry.name);
const fileStat = await fs.stat(fullPath).catch(() => null);
return fileStat ? { path: fullPath, mtimeMs: fileStat.mtimeMs } : null;
}),
);
const sorted = candidates
.filter((entry): entry is NonNullable<typeof entry> => Boolean(entry))
.sort((a, b) => b.mtimeMs - a.mtimeMs);
return sorted[0]?.path ?? file;
}
async function readLogSlice(params: {
file: string;
cursor?: number;
@@ -126,8 +156,9 @@ export const logsHandlers: GatewayRequestHandlers = {
}
const p = params as { cursor?: number; limit?: number; maxBytes?: number };
const file = getResolvedLoggerSettings().file;
const configuredFile = getResolvedLoggerSettings().file;
try {
const file = await resolveLogFile(configuredFile);
const result = await readLogSlice({
file,
cursor: p.cursor,