InsightLogger is a powerful, enterprise-grade logging and monitoring library for Python applications. It provides comprehensive insights into your application's performance, system health, and behavior with real-time monitoring, advanced analytics, and beautiful visualizations.
- π― Real-time System Monitoring: CPU, memory, and network usage tracking
- π Security Event Logging: Built-in security monitoring and event tracking
- π Interactive HTML Dashboards: Real-time web-based monitoring interface
- ποΈ Database Logging: SQLite integration for persistent log storage
- π§ Smart Alerting: Email notifications for critical events and thresholds
- π Anomaly Detection: Automatic detection of performance and behavior anomalies
- π Advanced Analytics: Comprehensive performance profiling and bottleneck identification
- π¨ Enhanced Visualizations: Multi-type charts, graphs, and performance reports
- π§ Plugin System: Extensible architecture for custom functionality
- π€ Data Export: Multiple export formats (JSON, CSV) with comprehensive reports
- πͺ Context Managers: Easy-to-use context managers for performance profiling
- π₯ Health Scoring: Automatic calculation of system health scores
- πΎ Log Compression: Automatic compression of old log files
- π Batch Logging: Efficient processing of multiple log entries
- π·οΈ Multiple Log Levels: INFO, DEBUG, ERROR, SUCCESS, FAILURE, WARNING, ALERT, TRACE, HIGHLIGHT, CRITICAL
- π¨ Rich Formatting: Colors, emojis, borders, highlighting, and custom styling
- π Context Logging: Add metadata, tags, and contextual information
- π¦ Batch Processing: Efficiently process multiple log entries
- β‘ Function Timing: Automatic execution time tracking with decorators
- π Real-time Spinners: Live progress indicators during function execution
- πΎ Memory Tracking: Monitor memory usage and memory deltas
- π Performance Profiling: Detailed performance analysis and bottleneck identification
- π» Resource Monitoring: Real-time CPU and memory usage tracking
- π Network Monitoring: Network I/O statistics and monitoring
- π Custom Metrics: Track application-specific metrics and KPIs
- π API Monitoring: Track API call performance and response times
- π€ Anomaly Detection: Automatic detection of unusual patterns and behaviors
- π‘ Smart Recommendations: AI-powered optimization suggestions
- π₯ Health Scoring: Overall system health assessment (0-100 scale)
- π Comprehensive Reports: Detailed performance and analysis reports
- π Security Event Logging: Track security-related events and incidents
- π‘οΈ Data Masking: Automatic masking of sensitive information in logs
- π¨ Threat Detection: Monitor for security threats and suspicious activities
- π Advanced Charts: Bar charts, time series, pie charts, and performance graphs
- π HTML Dashboards: Interactive web-based monitoring interface
- π€ Export Capabilities: JSON, CSV, and custom format exports
- π Real-time Updates: Live updating charts and metrics
- ποΈ Database Integration: SQLite for persistent logging and analysis
- π§ Email Alerts: SMTP integration for critical event notifications
- π Plugin System: Extensible architecture for custom functionality
- πͺ Context Manager Support: Easy integration with existing code
The installation process remains unchanged to maintain compatibility:
-
Clone the repository:
git clone https://github.com/Velyzo/InsightLogger.git cd InsightLogger
-
Install dependencies:
pip install -r requirements.txt
-
Install the package:
pip install -e .
termcolor>=2.0.0
- Enhanced terminal colors and formattingmatplotlib>=3.5.0
- Advanced plotting and visualizationtabulate>=0.9.0
- Beautiful table formattingpsutil>=5.8.0
- System and process monitoringnumpy>=1.21.0
- Numerical computing for analyticstqdm>=4.64.0
- Progress bars and indicators
from insightlog import InsightLogger
# Initialize with enhanced features
logger = InsightLogger(
name="MyApp",
enable_database=True,
enable_monitoring=True,
enable_alerts=False
)
# Basic logging with enhanced formatting
logger.log_types("INFO", "Application started successfully", emoji=True, bold=True)
logger.log_types("SUCCESS", "Database connection established", border=True)
logger.log_types("WARNING", "Cache miss detected", background=True)
logger.log_types("ERROR", "Failed to connect to API", urgent=True)
# Function performance monitoring
@logger.log_function_time
def process_data():
import time
time.sleep(2) # Simulate work
return "Data processed"
result = process_data()
# View comprehensive insights
logger.view_insights(detailed=True, create_dashboard=True)
from insightlog import InsightLogger
# Use as context manager for automatic cleanup
with InsightLogger(
name="AdvancedApp",
enable_database=True,
enable_monitoring=True,
enable_alerts=True,
alert_email="admin@company.com",
smtp_server="smtp.gmail.com",
smtp_user="alerts@company.com",
smtp_password="your_password"
) as logger:
# Performance profiling
with logger.performance_profile("data_processing"):
# Your code here
data = [i**2 for i in range(100000)]
# Custom metrics
logger.add_custom_metric("user_count", 1500)
logger.add_custom_metric("active_sessions", 45)
# API monitoring
logger.track_api_call("/api/users", "GET", response_time=234, status_code=200)
# Security logging
logger.log_security_event("LOGIN_ATTEMPT", "LOW", "User login from new device")
# Contextual logging
logger.log_with_context(
"INFO",
"User action performed",
context={"user_id": 12345, "action": "file_upload"},
tags=["user_activity", "audit"]
)
# Batch logging
logs = [
{"level": "INFO", "message": "Processing item 1"},
{"level": "INFO", "message": "Processing item 2"},
("SUCCESS", "Batch processing completed")
]
logger.batch_log(logs)
# Automatic cleanup and final report generated
logger = InsightLogger(
name="MyLogger", # Logger name
save_log="enabled", # Enable/disable file logging
log_dir=".insight", # Log directory
log_filename="app.log", # Log filename
max_bytes=1000000, # Max log file size
backup_count=1, # Number of backup files
log_level=logging.DEBUG, # Logging level
enable_database=True, # Enable SQLite logging
enable_monitoring=True, # Enable system monitoring
enable_alerts=False, # Enable email alerts
alert_email="admin@example.com", # Alert email address
smtp_server="smtp.gmail.com", # SMTP server
smtp_port=587, # SMTP port
smtp_user="user@gmail.com", # SMTP username
smtp_password="password" # SMTP password
)
# Customize alert thresholds
logger.alert_thresholds = {
'cpu_usage': 80, # CPU usage percentage
'memory_usage': 85, # Memory usage percentage
'error_rate': 10, # Error rate percentage
'response_time': 5000 # Response time in milliseconds
}
Level | Description | Color | Emoji |
---|---|---|---|
INFO |
General information | Cyan | βΉοΈ |
SUCCESS |
Successful operations | Green | β |
WARNING |
Warning messages | Yellow | |
ERROR |
Error messages | Red | π₯ |
CRITICAL |
Critical errors | Red | π₯ |
DEBUG |
Debug information | Blue | π |
ALERT |
Alert notifications | Magenta | π¨ |
TRACE |
Trace information | Cyan | π |
HIGHLIGHT |
Important highlights | Yellow | β |
FAILURE |
Failed operations | Red | β |
# Enhanced formatting options
logger.log_types("INFO", "Message",
emoji=True, # Add emoji icons
bold=True, # Bold text
underline=True, # Underlined text
border=True, # Add borders
background=True, # Background highlighting
urgent=True) # Blinking text
# Method 1: Decorator
@logger.log_function_time
def expensive_function():
# Your code here
pass
# Method 2: Context Manager
with logger.performance_profile("operation_name"):
# Your code here
pass
# Add custom application metrics
logger.add_custom_metric("active_users", 150)
logger.add_custom_metric("cache_hit_rate", 0.95)
logger.add_custom_metric("database_connections", 25)
# Track API performance
logger.track_api_call("/api/data", "POST", response_time=345, status_code=201)
# Monitor system resources (automatic)
# CPU, memory, and network usage tracked in background
# Log security events
logger.log_security_event("FAILED_LOGIN", "MEDIUM", "Multiple failed attempts")
logger.log_security_event("PRIVILEGE_ESCALATION", "HIGH", "Unauthorized access")
# Secure logging with data masking
from insightlog import secure_log_decorator
@secure_log_decorator(logger, mask_patterns=[r'\d{4}-\d{4}-\d{4}-\d{4}'])
def process_payment(card_number):
# Sensitive data automatically masked in logs
pass
# Query log database
filtered_logs = logger.create_log_filter(
level="ERROR",
start_time=datetime.datetime(2024, 1, 1),
end_time=datetime.datetime.now(),
function_name="process_data"
)
# Get function statistics
stats = logger.get_function_statistics()
for func_name, metrics in stats.items():
print(f"{func_name}: {metrics['avg_time']:.2f}ms average")
# Detect system anomalies
anomalies = logger.detect_anomalies()
for anomaly in anomalies:
print(f"β οΈ Anomaly detected: {anomaly}")
# Get system health score
health_score = logger._calculate_health_score()
print(f"System Health: {health_score}/100")
# Get optimization recommendations
recommendations = logger._generate_recommendations()
for rec in recommendations:
print(f"π‘ {rec['category']}: {rec['message']}")
# View detailed insights
logger.view_insights(
detailed=True, # Include detailed analysis
export_format="json", # Export data
create_dashboard=True # Generate HTML dashboard
)
# Generate advanced analytics report
report = logger.generate_advanced_report()
print(f"Health Score: {report['executive_summary']['health_score']}")
# Export to JSON with raw data
json_file = logger.export_data("json", include_raw_data=True)
# Export to CSV for analysis
csv_file = logger.export_data("csv")
# Create interactive HTML dashboard
dashboard_path = logger.create_dashboard_html()
- π Log Frequency Bar Charts: Distribution of log levels
- π System Resource Time Series: CPU and memory usage over time
- β‘ Function Performance Analysis: Execution times and call distributions
- π Performance Trends: Function performance over time
- π¨ Error Rate Analysis: Error rates by function
- π Interactive Dashboards: Real-time web interface
from flask import Flask
from insightlog import InsightLogger
app = Flask(__name__)
logger = InsightLogger("FlaskApp", enable_monitoring=True)
@app.route('/api/data')
@logger.log_function_time
def get_data():
logger.track_api_call("/api/data", "GET",
response_time=234, status_code=200)
return {"data": "example"}
@app.errorhandler(500)
def handle_error(error):
logger.log_types("ERROR", f"Server error: {error}")
logger.log_security_event("SERVER_ERROR", "HIGH", str(error))
return "Internal Server Error", 500
from insightlog import InsightLogger, MetricsCollector
with InsightLogger("DataPipeline", enable_database=True) as logger:
metrics = MetricsCollector(logger)
# Track data processing stages
with metrics.time_operation("data_extraction"):
# Extract data
metrics.count_event("records_extracted")
with metrics.time_operation("data_transformation"):
# Transform data
metrics.gauge_value("data_quality_score", 0.95)
with metrics.time_operation("data_loading"):
# Load data
metrics.count_event("records_loaded")
from insightlog import InsightLogger
import requests
logger = InsightLogger("MicroService",
enable_alerts=True,
alert_email="ops@company.com")
def health_check():
"""Comprehensive service health check"""
try:
# Check database
with logger.performance_profile("db_health_check"):
# Database check logic
pass
# Check external APIs
response_time = logger.track_api_call("/health", "GET", 150, 200)
# Calculate and log health metrics
health_score = logger._calculate_health_score()
logger.add_custom_metric("service_health", health_score)
if health_score < 70:
logger.log_types("ALERT", f"Service health degraded: {health_score}/100")
return health_score > 80
except Exception as e:
logger.log_types("CRITICAL", f"Health check failed: {e}")
logger.log_security_event("HEALTH_CHECK_FAILURE", "HIGH", str(e))
return False
InsightLogger creates a comprehensive set of output files in the .insight
directory:
.insight/
βββ app.log # Standard log file
βββ insights_[session_id].db # SQLite database
βββ dashboard_[session_id].html # Interactive dashboard
βββ log_frequency_[timestamp].png # Log level frequency chart
βββ system_metrics_[timestamp].png # System resource usage
βββ function_performance_[timestamp].png # Function performance analysis
βββ insight_export_[timestamp].json # Exported data (JSON)
βββ insight_export_[timestamp].csv # Exported data (CSV)
βββ plugins/ # Custom plugins directory
# Production configuration
logger = InsightLogger(
name="ProductionApp",
log_level=logging.INFO, # Reduce verbosity
enable_database=True, # Enable for analytics
enable_monitoring=True, # Monitor system health
enable_alerts=True, # Enable critical alerts
alert_email="ops@company.com", # Operations team
max_bytes=10000000, # 10MB log files
backup_count=5 # Keep 5 backups
)
# Set production alert thresholds
logger.alert_thresholds = {
'cpu_usage': 85,
'memory_usage': 90,
'error_rate': 5,
'response_time': 3000
}
# Development configuration
logger = InsightLogger(
name="DevApp",
log_level=logging.DEBUG, # Verbose logging
enable_database=True, # For analysis
enable_monitoring=True, # Performance insights
enable_alerts=False, # No email alerts
create_dashboard=True # Visual debugging
)
# Testing configuration
logger = InsightLogger(
name="TestApp",
save_log="disabled", # No file logging
enable_database=False, # No persistence needed
enable_monitoring=False, # No system monitoring
enable_alerts=False # No alerts during tests
)
We welcome contributions to InsightLogger! Here's how you can help:
-
Fork the repository
-
Create a virtual environment:
python -m venv insight_env source insight_env/bin/activate # Linux/Mac # or insight_env\Scripts\activate # Windows
-
Install development dependencies:
pip install -r requirements.txt pip install -e .
-
Run tests:
python -m pytest tests/
- π Bug Reports: Use GitHub issues with detailed reproduction steps
- β¨ Feature Requests: Propose new features with use cases
- π Documentation: Improve docs and examples
- π§ͺ Testing: Add tests for new features
- π§ Code Quality: Follow PEP 8 and include type hints
- π Plugin System: Create custom plugins
- π Visualizations: New chart types and dashboards
- π Analytics: Advanced anomaly detection algorithms
- π Security: Enhanced security monitoring features
- π Integrations: New framework and service integrations
InsightLogger is licensed under the MIT License. See LICENSE for details.
- π Documentation: GitHub Wiki
- π Bug Tracker: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π§ Support: Email Support
InsightLogger v1.4 is developed and maintained by Velyzo.
- π GitHub: @Velyzo
- π§ Email: Velyzo.help@web.de
Special thanks to all contributors and the open-source community for making InsightLogger better with each release!
InsightLogger v1.4 - Powering the next generation of Python application monitoring and analytics π