| 38 | } |
| 39 | |
| 40 | class Logger { |
| 41 | constructor( |
| 42 | public readonly logFilePath = getSessionLogPath() |
| 43 | ) {} |
| 44 | |
| 45 | localTimezoneTimestamp(): string { |
| 46 | return createTimestampForLogEntry() |
| 47 | } |
| 48 | |
| 49 | debug(message: string, ...args: unknown[]): void { |
| 50 | this.logToFile(`[${this.localTimezoneTimestamp()}]`, message, ...args) |
| 51 | } |
| 52 | |
| 53 | debugLargeJson( |
| 54 | message: string, |
| 55 | object: unknown, |
| 56 | maxStringLength: number = 100, |
| 57 | maxArrayLength: number = 10, |
| 58 | ): void { |
| 59 | if (!process.env.DEBUG) { |
| 60 | this.debug(`In production, skipping message inspection`) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | const truncateStrings = (obj: unknown): unknown => { |
| 65 | if (typeof obj === 'string') { |
| 66 | return obj.length > maxStringLength |
| 67 | ? obj.substring(0, maxStringLength) + '... [truncated]' |
| 68 | : obj |
| 69 | } |
| 70 | if (Array.isArray(obj)) { |
| 71 | const truncatedArray = obj.map(item => truncateStrings(item)).slice(0, maxArrayLength) |
| 72 | if (obj.length > maxArrayLength) { |
| 73 | truncatedArray.push(`... [truncated array up to ${maxArrayLength} items]` as unknown) |
| 74 | } |
| 75 | return truncatedArray |
| 76 | } |
| 77 | if (obj && typeof obj === 'object') { |
| 78 | const result: Record<string, unknown> = {} |
| 79 | for (const [key, value] of Object.entries(obj)) { |
| 80 | if (key === 'usage') continue |
| 81 | result[key] = truncateStrings(value) |
| 82 | } |
| 83 | return result |
| 84 | } |
| 85 | return obj |
| 86 | } |
| 87 | |
| 88 | const truncatedObject = truncateStrings(object) |
| 89 | const json = JSON.stringify(truncatedObject, null, 2) |
| 90 | this.logToFile(`[${this.localTimezoneTimestamp()}]`, message, '\n', json) |
| 91 | } |
| 92 | |
| 93 | info(message: string, ...args: unknown[]): void { |
| 94 | this.logToConsole('info', '', message, ...args) |
| 95 | this.debug(message, args) |
| 96 | } |
| 97 |
nothing calls this directly
no outgoing calls
no test coverage detected