|
| 1 | +// Application logger utility |
| 2 | +type LogLevel = 'debug' | 'info' | 'warn' | 'error'; |
| 3 | + |
| 4 | +const LOG_COLORS: Record<LogLevel, string> = { |
| 5 | + debug: 'color: #9CA3AF', |
| 6 | + info: 'color: #3B82F6', |
| 7 | + warn: 'color: #F59E0B', |
| 8 | + error: 'color: #EF4444', |
| 9 | +}; |
| 10 | + |
| 11 | +class Logger { |
| 12 | + private isDev = import.meta.env?.DEV ?? true; |
| 13 | + |
| 14 | + private log(level: LogLevel, message: string, data?: unknown): void { |
| 15 | + if (!this.isDev && level === 'debug') return; |
| 16 | + const timestamp = new Date().toISOString().slice(11, 23); |
| 17 | + const prefix = '[' + timestamp + '] [' + level.toUpperCase() + ']'; |
| 18 | + if (data) { console.log('%c' + prefix + ' ' + message, LOG_COLORS[level], data); } |
| 19 | + else { console.log('%c' + prefix + ' ' + message, LOG_COLORS[level]); } |
| 20 | + } |
| 21 | + |
| 22 | + debug(msg: string, data?: unknown) { this.log('debug', msg, data); } |
| 23 | + info(msg: string, data?: unknown) { this.log('info', msg, data); } |
| 24 | + warn(msg: string, data?: unknown) { this.log('warn', msg, data); } |
| 25 | + error(msg: string, data?: unknown) { this.log('error', msg, data); } |
| 26 | +} |
| 27 | + |
| 28 | +export const logger = new Logger(); |
0 commit comments