* Logger service for capturing and managing application logs
| 29 | * Logger service for capturing and managing application logs |
| 30 | */ |
| 31 | class LoggingService { |
| 32 | private logs: LogEntry[] = []; |
| 33 | private listeners: LogListener[] = []; |
| 34 | private maxLogEntries: number = 1000; // Maximum number of logs to keep in memory |
| 35 | private nextLogId: number = 1; |
| 36 | |
| 37 | constructor() { |
| 38 | // Initialize with any saved logs if needed |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Add a new log entry |
| 43 | */ |
| 44 | public log( |
| 45 | level: LogLevel, |
| 46 | source: string, |
| 47 | message: string, |
| 48 | details?: any |
| 49 | ): LogEntry { |
| 50 | const entry: LogEntry = { |
| 51 | id: `log-${this.nextLogId++}`, |
| 52 | timestamp: new Date(), |
| 53 | level, |
| 54 | source, |
| 55 | message, |
| 56 | details |
| 57 | }; |
| 58 | |
| 59 | // Add to log array |
| 60 | this.logs.push(entry); |
| 61 | |
| 62 | // Trim logs if we exceed max size |
| 63 | if (this.logs.length > this.maxLogEntries) { |
| 64 | this.logs = this.logs.slice(-this.maxLogEntries); |
| 65 | } |
| 66 | |
| 67 | // Notify all listeners |
| 68 | this.notifyListeners(entry); |
| 69 | |
| 70 | return entry; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Convenience method for debug logs |
| 75 | */ |
| 76 | public debug(source: string, message: string, details?: any): LogEntry { |
| 77 | return this.log(LogLevel.DEBUG, source, message, details); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Convenience method for info logs |
| 82 | */ |
| 83 | public info(source: string, message: string, details?: any): LogEntry { |
| 84 | return this.log(LogLevel.INFO, source, message, details); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Convenience method for warning logs |
nothing calls this directly
no outgoing calls
no test coverage detected