Files
platform/src/servers/api/pi/logger.ts
T

58 lines
1.3 KiB
TypeScript

/**
* Structured logging utility for Pi harness
*/
export type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR';
type LogContext = {
sessionId?: string;
email?: string;
model?: string;
requestId?: string;
[key: string]: unknown;
};
const LOG_COLORS = {
DEBUG: '\x1b[36m', // Cyan
INFO: '\x1b[32m', // Green
WARN: '\x1b[33m', // Yellow
ERROR: '\x1b[31m', // Red
RESET: '\x1b[0m',
};
function formatTimestamp(): string {
return new Date().toISOString();
}
function formatContext(context?: LogContext): string {
if (!context || Object.keys(context).length === 0) return '';
return ' ' + JSON.stringify(context);
}
function log(level: LogLevel, message: string, context?: LogContext) {
const timestamp = formatTimestamp();
const color = LOG_COLORS[level];
const reset = LOG_COLORS.RESET;
const contextStr = formatContext(context);
console.log(`${color}[${timestamp}] [Pi] [${level}]${reset} ${message}${contextStr}`);
}
export const logger = {
debug(message: string, context?: LogContext) {
log('DEBUG', message, context);
},
info(message: string, context?: LogContext) {
log('INFO', message, context);
},
warn(message: string, context?: LogContext) {
log('WARN', message, context);
},
error(message: string, context?: LogContext) {
log('ERROR', message, context);
},
};