| 6 | } |
| 7 | |
| 8 | export class Logger { |
| 9 | private level: LogLevel; |
| 10 | private customLogger?: (level: LogLevel, ...args: any[]) => void; |
| 11 | |
| 12 | private readonly logLevels: Record<LogLevel, number> = { |
| 13 | 'verbose': 0, |
| 14 | 'debug': 1, |
| 15 | 'info': 2, |
| 16 | 'warn': 3, |
| 17 | 'error': 4, |
| 18 | 'none': 5, // Disable logging |
| 19 | }; |
| 20 | |
| 21 | constructor(options: Partial<LoggerOptions> = {}) { |
| 22 | this.level = options.level || 'info'; |
| 23 | this.customLogger = options.customLogger; |
| 24 | } |
| 25 | |
| 26 | public setLevel(level: LogLevel): void { |
| 27 | this.level = level; |
| 28 | } |
| 29 | |
| 30 | public setCustomLogger(logger: (level: LogLevel, ...args: any[]) => void): void { |
| 31 | this.customLogger = logger; |
| 32 | } |
| 33 | |
| 34 | public verbose(...args: any[]): void { |
| 35 | this.log('verbose', ...args); |
| 36 | } |
| 37 | |
| 38 | public debug(...args: any[]): void { |
| 39 | this.log('debug', ...args); |
| 40 | } |
| 41 | |
| 42 | public info(...args: any[]): void { |
| 43 | this.log('info', ...args); |
| 44 | } |
| 45 | |
| 46 | public warn(...args: any[]): void { |
| 47 | this.log('warn', ...args); |
| 48 | } |
| 49 | |
| 50 | public error(...args: any[]): void { |
| 51 | this.log('error', ...args); |
| 52 | } |
| 53 | |
| 54 | private log(level: LogLevel, ...args: any[]): void { |
| 55 | // Skip logging if the level is too low or set to none |
| 56 | if (this.logLevels[level] < this.logLevels[this.level]) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if (this.customLogger) { |
| 61 | this.customLogger(level, ...args); |
| 62 | } else { |
| 63 | // Default logging implementation |
| 64 | switch (level) { |
| 65 | default: |
nothing calls this directly
no outgoing calls
no test coverage detected