export type LogLevel = 'error' | 'warn' | 'info' | 'debug';
export class Logger {
private level: LogLevel;
private levels: Record<LogLevel, number> = {
error: 0,
warn: 1,
info: 2,
debug: 3,
};
constructor(level: LogLevel = 'info') {
this.level = level;
}
private shouldLog(level: LogLevel): boolean {
return this.levels[level] <= this.levels[this.level];
}
private formatMessage(level: LogLevel, message: string, ...args: any[]): string {
const timestamp = new Date().toISOString();
const formattedArgs =
args.length > 0
? ' ' + args.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg))).join(' ')
: '';
return `[${timestamp}] [${level.toUpperCase()}] ${message}${formattedArgs}`;
}
public error(message: string, ...args: any[]): void {
if (this.shouldLog('error')) {
console.error(this.formatMessage('error', message, ...args));
}
}
public warn(message: string, ...args: any[]): void {
if (this.shouldLog('warn')) {
console.warn(this.formatMessage('warn', message, ...args));
}
}
public info(message: string, ...args: any[]): void {
if (this.shouldLog('info')) {
console.info(this.formatMessage('info', message, ...args));
}
}
public debug(message: string, ...args: any[]): void {
if (this.shouldLog('debug')) {
console.debug(this.formatMessage('debug', message, ...args));
}
}
public setLevel(level: LogLevel): void {
this.level = level;
}
public getLevel(): LogLevel {
return this.level;
}
}