Project Files
src / logger.ts
import * as fs from "fs";
import * as path from "path";
const LOGS_DIR = path.join(__dirname, "..", "..", ".logs");
const MAX_LOG_SIZE_BYTES = 1024 * 1024; // 1MB
// Ensure logs directory exists
if (!fs.existsSync(LOGS_DIR)) {
fs.mkdirSync(LOGS_DIR, { recursive: true });
}
export function logDebug(message: string): void {
try {
const logFile = path.join(LOGS_DIR, "terminal-debug.log");
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] ${message}\n`;
// Check file size and rotate if needed
if (fs.existsSync(logFile)) {
const stats = fs.statSync(logFile);
if (stats.size > MAX_LOG_SIZE_BYTES) {
const backupFile = `${logFile}.${Date.now()}`;
fs.renameSync(logFile, backupFile);
}
}
fs.appendFileSync(logFile, logEntry);
} catch (err) {
// Silently fail - logging should never break functionality
console.error("Failed to write debug log:", err);
}
}
export function formatCommandForLog(command: string, maxLength = 200): string {
if (command.length <= maxLength) return command;
return command.slice(0, maxLength) + "…";
}