refactor: scope runtime log context
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user