Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/lib/consoleCapture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export interface ConsoleLogEntry {
timestamp: string;
level: 'LOG' | 'INFO' | 'WARN' | 'ERROR' | 'DEBUG';
message: string;
}

const MAX_ENTRIES = 2000;
const entries: ConsoleLogEntry[] = [];

function serializeArgs(args: unknown[]): string {
return args
.map((a) => {
if (typeof a === 'string') return a;
try {
return JSON.stringify(a);
} catch {
return String(a);
}
})
.join(' ');
}

// JS Date only has ms precision (.234Z). Pad to 6 decimal places to match
// the backend's microsecond format (.234000Z) so timestamps sort and display uniformly.
function timestamp(): string {
return new Date().toISOString().replace(/\.(\d{3})Z$/, '.$1000Z');
}

function capture(level: ConsoleLogEntry['level'], args: unknown[]) {
entries.push({
timestamp: timestamp(),
level,
message: serializeArgs(args),
});
if (entries.length > MAX_ENTRIES) {
entries.splice(0, entries.length - MAX_ENTRIES);
}
}

let installed = false;

export function installConsoleCapture() {
if (installed) return;
installed = true;

const orig = {
log: console.log.bind(console),
info: console.info.bind(console),
warn: console.warn.bind(console),
error: console.error.bind(console),
debug: console.debug.bind(console),
};

console.log = (...args: unknown[]) => {
capture('LOG', args);
orig.log(...args);
};
console.info = (...args: unknown[]) => {
capture('INFO', args);
orig.info(...args);
};
console.warn = (...args: unknown[]) => {
capture('WARN', args);
orig.warn(...args);
};
console.error = (...args: unknown[]) => {
capture('ERROR', args);
orig.error(...args);
};
console.debug = (...args: unknown[]) => {
capture('DEBUG', args);
orig.debug(...args);
};
}

export function getConsoleLogs(): ConsoleLogEntry[] {
return [...entries];
}

export function clearConsoleLogs() {
entries.length = 0;
}
Loading
Loading