fix: add timestamped tool context to logs
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
enableConsoleCapture,
|
||||
resetLogger,
|
||||
routeLogsToStderr,
|
||||
setConsoleTimestampPrefix,
|
||||
setLoggerOverride,
|
||||
} from "../logging.js";
|
||||
import { loggingState } from "./state.js";
|
||||
@@ -34,6 +35,7 @@ beforeEach(() => {
|
||||
};
|
||||
loggingState.consolePatched = false;
|
||||
loggingState.forceConsoleToStderr = false;
|
||||
loggingState.consoleTimestampPrefix = false;
|
||||
loggingState.rawConsole = null;
|
||||
resetLogger();
|
||||
});
|
||||
@@ -47,6 +49,7 @@ afterEach(() => {
|
||||
console.trace = snapshot.trace;
|
||||
loggingState.consolePatched = false;
|
||||
loggingState.forceConsoleToStderr = false;
|
||||
loggingState.consoleTimestampPrefix = false;
|
||||
loggingState.rawConsole = null;
|
||||
resetLogger();
|
||||
setLoggerOverride(null);
|
||||
@@ -72,6 +75,43 @@ describe("enableConsoleCapture", () => {
|
||||
enableConsoleCapture();
|
||||
expect(() => console.log("hello")).not.toThrow();
|
||||
});
|
||||
|
||||
it("prefixes console output with timestamps when enabled", () => {
|
||||
setLoggerOverride({ level: "info", file: tempLogPath() });
|
||||
const now = new Date("2026-01-17T18:01:02.000Z");
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(now);
|
||||
const warn = vi.fn();
|
||||
console.warn = warn;
|
||||
setConsoleTimestampPrefix(true);
|
||||
enableConsoleCapture();
|
||||
console.warn("[EventQueue] Slow listener detected");
|
||||
expect(warn).toHaveBeenCalledTimes(1);
|
||||
const firstArg = String(warn.mock.calls[0]?.[0] ?? "");
|
||||
expect(firstArg.startsWith("2026-01-17T18:01:02.000Z [EventQueue]")).toBe(true);
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("does not double-prefix timestamps", () => {
|
||||
setLoggerOverride({ level: "info", file: tempLogPath() });
|
||||
const warn = vi.fn();
|
||||
console.warn = warn;
|
||||
setConsoleTimestampPrefix(true);
|
||||
enableConsoleCapture();
|
||||
console.warn("12:34:56 [exec] hello");
|
||||
expect(warn).toHaveBeenCalledWith("12:34:56 [exec] hello");
|
||||
});
|
||||
|
||||
it("leaves JSON output unchanged when timestamp prefix is enabled", () => {
|
||||
setLoggerOverride({ level: "info", file: tempLogPath() });
|
||||
const log = vi.fn();
|
||||
console.log = log;
|
||||
setConsoleTimestampPrefix(true);
|
||||
enableConsoleCapture();
|
||||
const payload = JSON.stringify({ ok: true });
|
||||
console.log(payload);
|
||||
expect(log).toHaveBeenCalledWith(payload);
|
||||
});
|
||||
});
|
||||
|
||||
function tempLogPath() {
|
||||
|
||||
Reference in New Issue
Block a user