UNPKG

loganalyzer-mcp

Version:

🚀 Debug server logs in under 30 seconds - AI-powered MCP server with rapid debugging, real-time monitoring, and actionable fixes

186 lines 7.55 kB
import { LogAnalyzer } from './logAnalyzer.js'; import { LogUtils } from '../utils.js'; export class RapidDebugger { logAnalyzer; constructor() { this.logAnalyzer = new LogAnalyzer(); } async debugInUnder30Seconds(logContent) { const startTime = Date.now(); // Phase 1: Instant Pattern Analysis (< 1 second) const patterns = this.instantPatternAnalysis(logContent); // Phase 2: AI Analysis (< 5 seconds) const aiAnalysis = await this.logAnalyzer.analyzeLogs(logContent, { logFormat: 'auto', contextLines: 20 // Reduced for speed }); // Phase 3: Generate Quick Fixes (< 2 seconds) const quickFixes = this.generateQuickFixes(patterns, aiAnalysis); // Phase 4: Debug Commands (< 1 second) const debugCommands = this.generateDebugCommands(patterns); const totalTime = Date.now() - startTime; return { timeToAnalysis: totalTime, criticalErrors: patterns.criticalErrors, quickFixes, debugCommands, rootCause: aiAnalysis.rootCause, confidence: aiAnalysis.confidence, nextSteps: this.generateNextSteps(patterns, aiAnalysis) }; } instantPatternAnalysis(logContent) { const lines = logContent.split('\n'); const errors = LogUtils.extractErrorPatterns(logContent); const stackTraces = LogUtils.extractStackTraces(logContent); // Rapid error classification const criticalErrors = []; const errorTypes = new Set(); for (const line of lines) { const lowerLine = line.toLowerCase(); // Database errors if (lowerLine.includes('database') && (lowerLine.includes('error') || lowerLine.includes('timeout'))) { criticalErrors.push('Database connection/timeout issues'); errorTypes.add('database'); } // Memory errors if (lowerLine.includes('outofmemory') || lowerLine.includes('heap space')) { criticalErrors.push('Memory exhaustion'); errorTypes.add('memory'); } // Network errors if (lowerLine.includes('connection refused') || lowerLine.includes('timeout')) { criticalErrors.push('Network connectivity issues'); errorTypes.add('network'); } // Authentication errors if (lowerLine.includes('unauthorized') || lowerLine.includes('authentication failed')) { criticalErrors.push('Authentication failures'); errorTypes.add('auth'); } // Configuration errors if (lowerLine.includes('config') && lowerLine.includes('error')) { criticalErrors.push('Configuration problems'); errorTypes.add('config'); } } return { criticalErrors: Array.from(new Set(criticalErrors)), errorTypes: Array.from(errorTypes), totalErrors: errors.length, hasStackTrace: stackTraces.length > 0, logLength: lines.length }; } generateQuickFixes(patterns, aiAnalysis) { const fixes = []; // Database fixes if (patterns.errorTypes.includes('database')) { fixes.push({ issue: 'Database Connection Issues', fix: 'Restart database service and check connection pool', command: 'sudo systemctl restart mysql && docker ps | grep database', priority: 'high', estimatedTime: '2-5 minutes' }); } // Memory fixes if (patterns.errorTypes.includes('memory')) { fixes.push({ issue: 'Memory Exhaustion', fix: 'Increase heap size and restart application', command: 'export JAVA_OPTS="-Xmx2g" && systemctl restart app', priority: 'high', estimatedTime: '1-3 minutes' }); } // Network fixes if (patterns.errorTypes.includes('network')) { fixes.push({ issue: 'Network Connectivity', fix: 'Check service health and network configuration', command: 'curl -I http://api-service:8080/health', priority: 'medium', estimatedTime: '30 seconds' }); } // Configuration fixes if (patterns.errorTypes.includes('config')) { fixes.push({ issue: 'Configuration Problems', fix: 'Validate and reload configuration', command: 'nginx -t && systemctl reload nginx', priority: 'medium', estimatedTime: '1 minute' }); } // Add AI-suggested fixes aiAnalysis.suggestedFixes.forEach((fix, index) => { if (index < 3) { // Limit for speed fixes.push({ issue: `AI Suggestion ${index + 1}`, fix, priority: 'medium', estimatedTime: '2-5 minutes' }); } }); return fixes; } generateDebugCommands(patterns) { const commands = [ '# Quick Health Check', 'systemctl status --no-pager', 'df -h | head -5', 'free -m', '', '# Recent Logs', 'journalctl -u myapp --since "5 minutes ago" --no-pager', 'tail -n 50 /var/log/app/error.log', '', '# Process Check', 'ps aux | grep -E "(java|python|node)" | head -5', 'netstat -tulpn | grep :8080' ]; // Add specific commands based on error types if (patterns.errorTypes.includes('database')) { commands.push('', '# Database Check', 'mysql -e "SHOW PROCESSLIST;" 2>/dev/null || echo "Database unreachable"'); } if (patterns.errorTypes.includes('network')) { commands.push('', '# Network Check', 'ping -c 3 api-service', 'curl -I http://localhost:8080/health'); } return commands; } generateNextSteps(patterns, aiAnalysis) { const steps = []; if (patterns.criticalErrors.length > 0) { steps.push('🚨 Address critical errors first (database, memory, network)'); } if (patterns.hasStackTrace) { steps.push('🔍 Examine stack traces for exact error locations'); } if (aiAnalysis.confidence > 80) { steps.push('🎯 High confidence analysis - follow AI suggestions'); } else { steps.push('❓ Low confidence - gather more context and logs'); } steps.push('📊 Monitor system metrics during fixes'); steps.push('✅ Test application functionality after each fix'); return steps; } // Quick analysis for real-time monitoring async quickScan(logContent) { const start = Date.now(); const errors = LogUtils.extractErrorPatterns(logContent); const critical = logContent.toLowerCase().includes('fatal') || logContent.toLowerCase().includes('critical') || errors.length > 5; return { errors: errors.length, critical, time: Date.now() - start }; } } //# sourceMappingURL=rapidDebugger.js.map