type LogLevel = 'debug' | 'info' | 'warn' | 'error';
class Logger {
private logLevel: LogLevel;
constructor() {
this.logLevel = (process.env.LOG_LEVEL as LogLevel) || 'warn';
}
private shouldLog(level: LogLevel): boolean {
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error'];
return levels.indexOf(level) >= levels.indexOf(this.logLevel);
}
private formatMessage(level: LogLevel, message: string, context?: Record<string, unknown>): string {
const timestamp = new Date().toISOString();
const contextStr = context ? ` ${JSON.stringify(context)}` : '';
return `[${timestamp}] [${level.toUpperCase()}] ${message}${contextStr}`;
}
debug(message: string, context?: Record<string, unknown>): void {
if (this.shouldLog('debug')) {
console.debug(this.formatMessage('debug', message, context));
}
}
info(message: string, context?: Record<string, unknown>): void {
if (this.shouldLog('info')) {
console.info(this.formatMessage('info', message, context));
}
}
warn(message: string, context?: Record<string, unknown>): void {
if (this.shouldLog('warn')) {
console.warn(this.formatMessage('warn', message, context));
}
}
error(message: string, context?: Record<string, unknown>): void {
if (this.shouldLog('error')) {
console.error(this.formatMessage('error', message, context));
}
}
}
export const logger = new Logger();