This document describes the comprehensive error logging system implemented for the UNGA Analysis application to help AI assistants better understand application issues.
The logging system provides:
- Structured JSON logging with detailed context
- Error tracking and analysis for pattern recognition
- AI-readable error summaries for better assistance
- Performance monitoring and health scoring
- Streamlit UI integration for real-time monitoring
- StructuredFormatter: Creates JSON-formatted log entries
- ErrorTracker: Tracks and analyzes error patterns
- LoggingConfig: Manages multiple loggers with file rotation
- Decorators:
@log_function_call,@log_streamlit_error - Context Managers:
log_operation()for operation tracking
- LogAnalyzer: Main analysis class
- Error pattern recognition and categorization
- Health score calculation (0-100)
- AI report generation for comprehensive understanding
- Suggested fixes based on error patterns
- Health Overview: System health score and metrics
- Error Analysis: Pattern analysis and categorization
- AI Insights: Comprehensive error report for AI understanding
- Performance Metrics: Operation timing and bottlenecks
The system automatically logs errors and provides AI-readable summaries. To get error insights:
from src.unga_analysis.utils.log_analyzer import get_error_insights, get_application_health
# Get quick health status
health = get_application_health()
print(health)
# Get comprehensive error analysis
insights = get_error_insights()
print(insights)Use the logging decorators in your functions:
from src.unga_analysis.utils.logging_config import log_function_call, log_operation
@log_function_call
def my_function():
# Function will be automatically logged with timing and error handling
pass
# Or use context manager
with log_operation("data_processing", "data", record_count=1000):
# Operation will be logged with context
process_data()Run the log analysis tool:
python analyze_logs.pyThis provides:
- Application health score
- Recent error summary
- AI-readable error report
- Suggested improvements
The system creates several log files in the logs/ directory:
app.log: General application logserrors.log: Error-specific logsperformance.log: Performance metricsapi.log: API call logsui.log: User interface logsdata.log: Data processing logserror_summary.json: Error pattern analysis
The system automatically categorizes errors:
- API Errors: OpenAI, rate limiting, authentication
- UI Errors: Streamlit, session management
- Data Errors: File processing, database queries
- Authentication Errors: Login, password issues
- Performance Issues: Timeouts, slow operations
- File Processing Errors: Upload, format issues
The health score (0-100) is calculated based on:
- Error frequency: More errors = lower score
- Error severity: Critical errors = bigger penalty
- Recent activity: Recent errors = higher impact
- Error diversity: More unique errors = lower score
Each error includes:
- Timestamp and duration
- Function/module information
- User session context
- Operation details
- File/request information
The system identifies:
- Recurring error patterns
- Component-specific issues
- Performance bottlenecks
- User behavior patterns
Based on error patterns, the system suggests:
- Code improvements
- Configuration changes
- Performance optimizations
- User experience enhancements
The error insights are available in the Streamlit UI:
- Error Insights Tab: Real-time error monitoring
- Health Dashboard: System status overview
- Performance Metrics: Operation timing analysis
- AI Report: Comprehensive error understanding
- Contextual Understanding: AI can see exactly what went wrong and when
- Pattern Recognition: Identify recurring issues and their causes
- Proactive Suggestions: Suggest fixes before users report issues
- Performance Optimization: Identify slow operations and bottlenecks
- User Experience: Understand user behavior and common issues
- Files rotate when they reach 10MB
- Keep 5 backup files
- Automatic cleanup of old logs
- Track error frequency and patterns
- Monitor error trends over time
- Identify critical issues quickly
- Track operation durations
- Identify performance bottlenecks
- Monitor resource usage
When an error occurs, the AI can now understand:
{
"timestamp": "2025-01-27T10:30:45",
"level": "ERROR",
"component": "api",
"operation": "openai_call",
"error_type": "RateLimitError",
"error_message": "Rate limit exceeded",
"context": {
"model": "gpt-4o",
"tokens_used": 15000,
"retry_count": 3,
"user_id": "user123"
},
"suggestions": [
"Implement exponential backoff",
"Add request queuing",
"Consider model fallback"
]
}This comprehensive logging system ensures that AI assistants can provide much better support by understanding the full context of application issues.