| 10 | type LogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' |
| 11 | |
| 12 | export class AiLogger { |
| 13 | private debugMode = false |
| 14 | private logDir: string |
| 15 | private logFile: string | null = null |
| 16 | private logStream: fs.WriteStream | null = null |
| 17 | |
| 18 | constructor(logsDir: string) { |
| 19 | this.logDir = path.join(logsDir, 'ai') |
| 20 | } |
| 21 | |
| 22 | setDebugMode(enabled: boolean): void { |
| 23 | this.debugMode = enabled |
| 24 | } |
| 25 | |
| 26 | isDebugMode(): boolean { |
| 27 | return this.debugMode |
| 28 | } |
| 29 | |
| 30 | debug(category: string, message: string, data?: unknown): void { |
| 31 | this.writeLog('DEBUG', category, message, data) |
| 32 | } |
| 33 | |
| 34 | info(category: string, message: string, data?: unknown): void { |
| 35 | this.writeLog('INFO', category, message, data) |
| 36 | } |
| 37 | |
| 38 | warn(category: string, message: string, data?: unknown): void { |
| 39 | this.writeLog('WARN', category, message, data) |
| 40 | } |
| 41 | |
| 42 | error(category: string, message: string, data?: unknown): void { |
| 43 | this.writeLog('ERROR', category, message, data) |
| 44 | } |
| 45 | |
| 46 | close(): void { |
| 47 | if (this.logStream) { |
| 48 | this.logStream.end() |
| 49 | this.logStream = null |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | getLogPath(): string { |
| 54 | return this.getLogFilePath() |
| 55 | } |
| 56 | |
| 57 | getExistingLogPath(): string | null { |
| 58 | if (this.logFile && fs.existsSync(this.logFile)) { |
| 59 | return this.logFile |
| 60 | } |
| 61 | return null |
| 62 | } |
| 63 | |
| 64 | private ensureLogDir(): void { |
| 65 | if (!fs.existsSync(this.logDir)) { |
| 66 | fs.mkdirSync(this.logDir, { recursive: true }) |
| 67 | } |
| 68 | } |
| 69 |
nothing calls this directly
no outgoing calls
no test coverage detected