type LogLevel = 'debug' | 'info' | 'warn' | 'error';
const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
debug: 0,
info: 1,
warn: 2,
error: 3,
};
let currentLogLevel: LogLevel = 'info';
export function configureLogger(logLevel: LogLevel): void {
currentLogLevel = logLevel;
}
function shouldLog(level: LogLevel): boolean {
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[currentLogLevel];
}
export const logger = {
debug: (message: string, ...args: unknown[]) => {
if (shouldLog('debug')) {
console.debug(`[DEBUG] ${message}`, ...args);
}
},
info: (message: string, ...args: unknown[]) => {
if (shouldLog('info')) {
console.info(`[INFO] ${message}`, ...args);
}
},
warn: (message: string, ...args: unknown[]) => {
if (shouldLog('warn')) {
console.warn(`[WARN] ${message}`, ...args);
}
},
error: (message: string, ...args: unknown[]) => {
if (shouldLog('error')) {
console.error(`[ERROR] ${message}`, ...args);
}
},
};