refactor: scope runtime log context

This commit is contained in:
Eyejoker
2026-03-31 17:23:43 +09:00
parent c2ef6572fd
commit 141898f764
8 changed files with 167 additions and 253 deletions

View File

@@ -1,4 +1,4 @@
import pino from 'pino';
import pino, { type Logger } from 'pino';
const serviceName = (process.env.ASSISTANT_NAME || 'claude').toLowerCase();
@@ -8,6 +8,27 @@ export const logger = pino({
transport: { target: 'pino-pretty', options: { colorize: true } },
});
type LogBindings = Record<string, unknown>;
function normalizeBindings(bindings: LogBindings): LogBindings {
const normalized = Object.fromEntries(
Object.entries(bindings).filter(([, value]) => value !== undefined),
);
if (
typeof normalized.groupName === 'string' &&
normalized.group === undefined
) {
normalized.group = normalized.groupName;
}
return normalized;
}
export function createScopedLogger(bindings: LogBindings): Logger {
return logger.child(normalizeBindings(bindings));
}
// Route uncaught errors through pino so they get timestamps in stderr
process.on('uncaughtException', (err) => {
logger.fatal({ err }, 'Uncaught exception');