/** Structured logger with worker context */ type LogLevel = 'info' | 'warn' | 'error' | 'debug'; interface LogEntry { level: LogLevel; ts: string; worker?: string; job?: string; account?: string; step?: string; msg: string; [key: string]: unknown; } let globalWorkerId = 'unknown'; export function setWorkerId(id: string): void { globalWorkerId = id; } function write(level: LogLevel, msg: string, extra?: Record): void { const entry: LogEntry = { level, ts: new Date().toISOString(), worker: globalWorkerId, msg, ...extra, }; const line = JSON.stringify(entry); if (level === 'error') { process.stderr.write(line + '\n'); } else { process.stdout.write(line + '\n'); } } export const logger = { info: (msg: string, extra?: Record) => write('info', msg, extra), warn: (msg: string, extra?: Record) => write('warn', msg, extra), error: (msg: string, extra?: Record) => write('error', msg, extra), debug: (msg: string, extra?: Record) => write('debug', msg, extra), /** Create a child logger with preset context fields */ child(context: Record) { return { info: (msg: string, extra?: Record) => write('info', msg, { ...context, ...extra }), warn: (msg: string, extra?: Record) => write('warn', msg, { ...context, ...extra }), error: (msg: string, extra?: Record) => write('error', msg, { ...context, ...extra }), debug: (msg: string, extra?: Record) => write('debug', msg, { ...context, ...extra }), }; }, };