File size: 1,929 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/* eslint-disable no-console */
import * as fs from 'node:fs';
import * as util from 'node:util';
/**
* A simple, centralized logger for developer-facing debug messages.
*
* WHY USE THIS?
* - It makes the INTENT of the log clear (it's for developers, not users).
* - It provides a single point of control for debug logging behavior.
* - We can lint against direct `console.*` usage to enforce this pattern.
*
* HOW IT WORKS:
* This is a thin wrapper around the native `console` object. The `ConsolePatcher`
* will intercept these calls and route them to the debug drawer UI.
*/
class DebugLogger {
private logStream: fs.WriteStream | undefined;
constructor() {
this.logStream = process.env['GEMINI_DEBUG_LOG_FILE']
? fs.createWriteStream(process.env['GEMINI_DEBUG_LOG_FILE'], {
flags: 'a',
})
: undefined;
// Handle potential errors with the stream
this.logStream?.on('error', (err) => {
// Log to console as a fallback, but don't crash the app
console.error('Error writing to debug log stream:', err);
});
}
private writeToFile(level: string, args: unknown[]) {
if (this.logStream) {
const message = util.format(...args);
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] [${level}] ${message}\n`;
this.logStream.write(logEntry);
}
}
log(...args: unknown[]): void {
this.writeToFile('LOG', args);
console.log(...args);
}
warn(...args: unknown[]): void {
this.writeToFile('WARN', args);
console.warn(...args);
}
error(...args: unknown[]): void {
this.writeToFile('ERROR', args);
console.error(...args);
}
debug(...args: unknown[]): void {
this.writeToFile('DEBUG', args);
console.debug(...args);
}
}
export const debugLogger = new DebugLogger();
|