Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/__tests__/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('level-gated methods do not emit when disabled', async () => {
assert.equal(debugCalls.length, 0, 'debug should be suppressed at warn level');
assert.equal(infoCalls.length, 0, 'info should be suppressed at warn level');
assert.equal(warnCalls.length, 1, 'warn should pass at warn level');
assert.match(warnCalls[0], /\[WARN\] \[test\/ctx\] warn-detail/);
assert.match(warnCalls[0], /^\[\d{4}-\d{2}-\d{2}T[^\]]+\] WARN \[test\/ctx\] warn-detail$/);
} finally {
console.debug = origDebug;
console.info = origInfo;
Expand All @@ -86,7 +86,7 @@ test('debug emits when level is debug', async () => {
logger.setLevel('debug');
logger.debug('test/ctx', 'detail');
assert.equal(calls.length, 1);
assert.match(calls[0], /\[DEBUG\] \[test\/ctx\] detail/);
assert.match(calls[0], /^\[\d{4}-\d{2}-\d{2}T[^\]]+\] DEBUG \[test\/ctx\] detail$/);
} finally {
console.debug = orig;
}
Expand All @@ -103,7 +103,7 @@ test('error always emits at any level that includes error', async () => {
calls.length = 0;
await logger.error('test/ctx', 'err-detail');
assert.equal(calls.length, 1, `error should fire at level=${level}`);
assert.match(calls[0], /\[ERROR\] \[test\/ctx\] err-detail/);
assert.match(calls[0], /^\[\d{4}-\d{2}-\d{2}T[^\]]+\] ERROR \[test\/ctx\] err-detail$/);
}
} finally {
console.error = orig;
Expand Down
8 changes: 2 additions & 6 deletions src/core/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@ function formatEntry(level: string, context: string, detail: string): string {
return `[${ts}] ${level} [${sanitize(context)}] ${sanitize(detail)}\n`;
}

function formatLine(level: string, context: string, detail: string): string {
return `[${level}] [${sanitize(context)}] ${sanitize(detail)}`;
}

function shouldEmit(level: LogLevel): boolean {
return LEVELS[level] >= currentLevel;
}

function emit(level: LogLevel, context: string, detail: string, sink: (line: string) => void) {
if (!shouldEmit(level)) return;
sink(formatLine(level.toUpperCase(), context, detail));
sink(formatEntry(level.toUpperCase(), context, detail).trimEnd());
}

export const logger = {
Expand All @@ -68,7 +64,7 @@ export const logger = {
emit('warn', context, detail, (line) => console.warn(line));
},
async error(context: string, detail: string): Promise<void> {
if (shouldEmit('error')) console.error(formatLine('ERROR', context, detail));
if (shouldEmit('error')) console.error(formatEntry('ERROR', context, detail).trimEnd());
try {
await ensureDir();
await appendFile(LOG_FILE, formatEntry('ERROR', context, detail));
Expand Down