Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ stand in this document. There is no project OMP configuration.
authoriser in `src/authorize`, tests in `test/`.
- Adapters (database, auth, storage, and so on) live in their own packages
under `packages/adapters/*`; `config-env` (reads the environment into the typed
config, the only place that reads `process.env`) exists, the others arrive later. Apps live in `apps/*`: `apps/web`
config, the only place that reads `process.env`) and `logger-json` (JSON logs with a
secret scrubber that fails closed) exist, the others arrive later. Apps live in `apps/*`: `apps/web`
(the web client) and `packages/api-client` exist today as empty placeholders.
- `tools/ci`: repository checks, with their own tests (`bun run test:tools`).
- `.github/workflows`: CI. Change it only on purpose.
Expand Down
36 changes: 36 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions packages/adapters/logger-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@tektum/grimoire-adapter-logger-json",
"version": "0.0.0",
"private": true,
"license": "Apache-2.0",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"dependencies": {
"@tektum/grimoire-core": "*",
"pino": "10.4.0"
}
}
72 changes: 72 additions & 0 deletions packages/adapters/logger-json/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { pino } from 'pino';
import type { Logger as PinoLogger } from 'pino';
import type { Clock } from '@tektum/grimoire-core/ports/clock';
import type { Logger, LogLevel } from '@tektum/grimoire-core/ports/logger';
import { redactString, scrubFields } from './scrub.js';

export { redactString } from './scrub.js';

export interface JsonLoggerOptions {
readonly level?: LogLevel;
/** Receives pino's output (one JSON line ending in a newline). Defaults to standard output. */
readonly destination?: { write(line: string): unknown };
readonly clock?: Clock;
readonly fields?: Record<string, unknown>;
}

// First layer: pino's own path redaction for the known secret names (one level deep).
const SECRET_NAMES = ['token', 'password', 'passwd', 'pwd', 'secret', 'authorization', 'cookie', 'apikey', 'api_key', 'access_key', 'secret_key', 'session', 'credential', 'credentials'];
const REDACT_PATHS = SECRET_NAMES.flatMap((n) => [n, `*.${n}`]);

interface TsState { value: string }

function wrap(p: PinoLogger, ts: TsState, now: () => string | undefined): Logger {
const emit = (level: LogLevel, event: string, fields?: Record<string, unknown>): void => {
try {
const t = now();
ts.value = t ?? new Date().toISOString();
const clean = scrubFields(fields);
if (t === undefined) clean['clockFallback'] = true;
p[level](clean, redactString(String(event)));
} catch {
// The logger never throws into its caller.
}
};
return {
debug: (e, f) => emit('debug', e, f),
info: (e, f) => emit('info', e, f),
warn: (e, f) => emit('warn', e, f),
error: (e, f) => emit('error', e, f),
child: (fields) => {
try {
return wrap(p.child(scrubFields(fields)), ts, now);
} catch {
return wrap(p, ts, now);
}
},
};
}

export function createJsonLogger(opts: JsonLoggerOptions = {}): Logger {
const clock = opts.clock;
// The clock is read once per event; an unusable reading falls back to real time and the event
// carries `clockFallback: true`.
const now = (): string | undefined => {
try {
return new Date(clock ? clock.now() : Date.now()).toISOString();
} catch {
return undefined;
}
};
const ts: TsState = { value: new Date().toISOString() };
const options = {
level: opts.level ?? 'info',
messageKey: 'event',
base: null,
timestamp: (): string => `,"ts":"${ts.value}"`,
formatters: { level: (label: string) => ({ level: label }) },
redact: { paths: REDACT_PATHS, censor: '[redacted]' },
};
const root = opts.destination ? pino(options, opts.destination) : pino(options);
return wrap(opts.fields ? root.child(scrubFields(opts.fields)) : root, ts, now);
}
Loading