feat(observability): an injectable logger, and a lint rule that keeps core off console

Task 4. createApp({ logger }) accepts any Logger; the default writes structured
JSON, or a readable line on a TTY, decided once at construction rather than per
record. It writes through console, which is what lets core have a logging
contract while reflect-metadata stays the only runtime dependency.

Four levels, because four have callers. Core's diagnostics were all warn — every
one of the eight console.warn calls — and request logging adds info and error.
No trace, no fatal: a level nothing emits is a decision handed to the user for
no reason.

Registered rather than declared as a provider. Provider boot failures are
reported while the graph is still resolving and shutdown warnings after it is
gone, so a logger that lives in the graph cannot cover either end. It is also
registered as a builtin provider, so @needs('logger') reaches the same object —
one instance, two access paths — and exposed on App so serveDeno/serveBun report
through the application's logger instead of past it.

The [green-tea] prefix moves out of the prose into a `name` field: an aggregator
filters on a field and cannot filter on a substring of a sentence. Context that
was interpolated into messages is now structured alongside them.

#10 asks that nothing in core call console directly. That is now a lint rule
scoped to src/ rather than a grep somebody has to remember, with src/logger.ts
exempted because being the console sink is its entire job. Verified it fails on
a planted console.log.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 14:51:33 -05:00
1 parent 7b0a3d4e24
commit df460f01dd
8 files changed
+316 -26

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { createApp, createDefaultLogger, Route, Get, Step, Module, Provider, needs } from '../src/index';
import type { Logger, LogFields } from '../src/index';
/** Captures every call so a test can assert on what core wrote rather than on console output. */
function recordingLogger(): Logger & { records: Array<{ level: string; message: string; fields?: LogFields }> } {
const records: Array<{ level: string; message: string; fields?: LogFields }> = [];
const at =
(level: string) =>
(message: string, fields?: LogFields): void => {
records.push({ level, message, fields });
};
return { records, debug: at('debug'), info: at('info'), warn: at('warn'), error: at('error') };
}
afterEach(() => {
vi.restoreAllMocks();
});
describe('default logger', () => {
it('writes one JSON object per record when not on a TTY', () => {
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {});
createDefaultLogger(false).warn('something happened', { route: '/x', count: 2 });
expect(spy).toHaveBeenCalledTimes(1);
const written = JSON.parse(spy.mock.calls[0][0] as string);
expect(written).toMatchObject({ level: 'warn', name: 'green-tea', msg: 'something happened', route: '/x', count: 2 });
// A log aggregator filters on a field; it cannot filter on a substring of prose.
expect(written.msg).not.toContain('[green-tea]');
expect(typeof written.time).toBe('string');
});
it('writes a readable line instead when interactive', () => {
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {});
createDefaultLogger(true).warn('something happened', { route: '/x' });
const written = spy.mock.calls[0][0] as string;
expect(written).toBe('[green-tea] WARN something happened route=/x');
});
it('routes each level to the matching console method', () => {
const spies = {
debug: vi.spyOn(console, 'debug').mockImplementation(() => {}),
info: vi.spyOn(console, 'info').mockImplementation(() => {}),
warn: vi.spyOn(console, 'warn').mockImplementation(() => {}),
error: vi.spyOn(console, 'error').mockImplementation(() => {}),
};
const logger = createDefaultLogger(false);
logger.debug('d');
logger.info('i');
logger.warn('w');
logger.error('e');
for (const spy of Object.values(spies)) expect(spy).toHaveBeenCalledTimes(1);
});
});
describe('createApp({ logger })', () => {
@Route('/')
class Ctl {
@Get('/ok')
ok() {
return { ok: true };
}
}
@Module({ mountpoint: '/', controllers: [Ctl] })
class M {}
it('sends framework diagnostics to the supplied logger, not to console', async () => {
const consoleWarn = vi.spyOn(console, 'warn').mockImplementation(() => {});
const logger = recordingLogger();
const app = createApp({ modules: [M], logger });
// close({ timeoutMs }) on an app that never listen()ed is one of the diagnostics core emits.
await app.close({ timeoutMs: 100 });
expect(logger.records).toHaveLength(1);
expect(logger.records[0].level).toBe('warn');
expect(logger.records[0].message).toContain('has no listen()ed server');
// The point of the contract: nothing reached console behind the logger's back.
expect(consoleWarn).not.toHaveBeenCalled();
});
it('exposes the logger on the app so adapters and plugins share it', () => {
const logger = recordingLogger();
expect(createApp({ modules: [M], logger }).logger).toBe(logger);
});
it('warns about a deep dependency chain through the logger, with structured fields', () => {
const logger = recordingLogger();
@Step({ provides: 's0' })
class S0 {
run() {
return { s0: 0 };
}
}
@Step({ provides: 's1', needs: ['s0'] })
class S1 {
run() {
return { s1: 1 };
}
}
@Route('/deep')
class DeepCtl {
@Get('/go')
go(@needs('s1') s: unknown) {
return { s };
}
}
@Module({ mountpoint: '/', steps: [S0, S1], controllers: [DeepCtl] })
class DeepModule {}
createApp({ modules: [DeepModule], logger, warnGraphDepth: 1 });
const warned = logger.records.find((r) => r.message.includes('unusually deep'));
expect(warned).toBeDefined();
// Structured, so a dashboard can group by route instead of parsing the sentence.
expect(warned!.fields).toMatchObject({ route: '/deep/go', method: 'GET', depth: 2, threshold: 1 });
});
});
describe('logger as a dependency', () => {
it('is injectable with @needs("logger") and is the same instance', async () => {
const logger = recordingLogger();
let seen: unknown;
@Provider({ provides: 'noop' })
class Noop {
provide() {
return { noop: 1 };
}
}
@Route('/')
class Ctl {
@Get('/log')
log(@needs('logger') injected: Logger) {
seen = injected;
injected.info('from a handler', { where: 'handler' });
return { ok: true };
}
}
@Module({ mountpoint: '/', providers: [Noop], controllers: [Ctl] })
class M {}
const app = createApp({ modules: [M], logger });
await app.fetch(new Request('http://x/log'));
// One object, two access paths — not a second logger wired into the graph.
expect(seen).toBe(logger);
expect(logger.records).toContainEqual({ level: 'info', message: 'from a handler', fields: { where: 'handler' } });
});
});