UNPKG

sourcewizard

Version:

SourceWizard - AI-powered setup wizard for dev tools and libraries with MCP integration

92 lines (91 loc) 3.21 kB
import fs from 'fs'; import path from 'path'; import os from 'os'; export class Logger { static logDir; static initialized = false; static initializeLogDir() { if (this.initialized) return; // Use ~/.config/sourcewizard for logs this.logDir = path.join(os.homedir(), '.config', 'sourcewizard', 'logs'); // Create directory if it doesn't exist try { fs.mkdirSync(this.logDir, { recursive: true }); } catch (error) { console.error('Failed to create log directory:', error); } this.initialized = true; } static getLogFilePath(type) { this.initializeLogDir(); const date = new Date().toISOString().split('T')[0]; // YYYY-MM-DD return path.join(this.logDir, `${type}-${date}.log`); } static writeLog(type, message, data) { const timestamp = new Date().toISOString(); const logEntry = { timestamp, type, message, ...(data && { data }) }; const logLine = JSON.stringify(logEntry) + '\n'; const logFile = this.getLogFilePath(type); try { fs.appendFileSync(logFile, logLine); } catch (error) { console.error('Failed to write to log file:', error); } } static logError(message, error, context) { const errorData = { message, ...(error && { error: { message: error.message, stack: error.stack, name: error.name, ...(error.cause && { cause: error.cause }) } }), ...(context && { context }) }; this.writeLog('error', message, errorData); // Also log to console for immediate visibility console.error(`[${new Date().toISOString()}] ERROR: ${message}`, error); } static logInstallationError(packageName, error, context) { const installError = { packageName, error: { message: error.message, stack: error.stack, name: error.name, ...(error.cause && { cause: error.cause }) }, ...(context && { context }) }; this.writeLog('error', `Installation failed for package: ${packageName}`, installError); this.writeLog('install', `Failed: ${packageName}`, installError); // Also log to console console.error(`[${new Date().toISOString()}] INSTALL ERROR: Package ${packageName} failed:`, error); } static logInfo(message, data) { this.writeLog('general', message, data); } static logInstallationSuccess(packageName, context) { const successData = { packageName, ...(context && { context }) }; this.writeLog('install', `Success: ${packageName}`, successData); console.log(`[${new Date().toISOString()}] INSTALL SUCCESS: Package ${packageName} installed successfully`); } static getLogDirectory() { this.initializeLogDir(); return this.logDir; } }