Interactive log viewer with charts and visualizations for FastAPI and Flask
Project description
LogLensx - Interactive Log Viewer
A powerful, interactive log viewer and analyzer for Python web applications. loglensx provides beautiful dashboards with real-time charts, tables, and analytics for your application logs.
Features
✨ Interactive Dashboards
- Real-time log visualization with charts and graphs
- Log level distribution (pie charts)
- Error frequency timeline
- Top loggers analysis
📊 Rich Analytics
- Log statistics and summary
- Error and warning frequency tracking
- Logger distribution analysis
- Full-text search across logs
🚀 Framework Integration
- FastAPI - Add loglensx with a single function call
- Flask - Seamless Flask integration
- Works with any Python logging setup
🎨 Beautiful UI
- Responsive design with Plotly charts
- Dark/Light theme support
- Mobile-friendly interface
- Real-time updates
Installation
Install loglensx from PyPI:
pip install loglensx
With Framework Support
FastAPI:
pip install loglensx[fastapi]
Flask:
pip install loglensx[flask]
Development:
pip install loglensx[dev]
Quick Start
FastAPI Integration
from fastapi import FastAPI
from loglensx import setup_fastapi_loglensx
app = FastAPI()
# Setup loglensx at your preferred URL prefix
setup_fastapi_loglensx(app, log_dir="logs", prefix="/loglensx")
# Now access the dashboard at http://localhost:8000/loglensx/
Flask Integration
from flask import Flask
from loglensx import setup_flask_loglens
app = Flask(__name__)
# Setup LogLens
setup_flask_loglens(app, log_dir="logs", prefix="/loglens")
# Access the dashboard at http://localhost:5000/loglens/
Standalone Usage
from loglensx import LogParser, LogAnalyzer
# Parse logs
parser = LogParser(log_dir="logs")
analyzer = LogAnalyzer(parser)
# Get statistics
summary = analyzer.get_log_summary()
print(f"Total logs: {summary['total_logs']}")
print(f"Errors: {summary['error_count']}")
# Get recent errors
recent_errors = analyzer.get_recent_errors(limit=5)
for error in recent_errors:
print(f"{error['timestamp']} - {error['message']}")
# Search logs
results = parser.search_logs("database", limit=50)
print(f"Found {len(results)} matching logs")
Log Format Support
loglensx automatically detects and parses common log formats:
[2024-01-15 10:30:45] [ERROR] [my_app.database] Connection timeout
[2024-01-15 10:30:46] [WARNING] [my_app.cache] Cache miss for key: user_123
[2024-01-15 10:30:47] [INFO] [my_app.api] GET /users/123 - 200
Custom format? loglensx accepts regex patterns for custom parsing.
API Endpoints
When integrated with FastAPI or Flask, loglensx exposes these API endpoints:
Dashboard
GET /loglens/- Main dashboard
API Endpoints
GET /loglens/api/logs?search=term&level=ERROR&limit=100- Get filtered logsGET /loglens/api/stats- Get statistics and summaryGET /loglens/api/search?q=query- Search logsGET /loglens/api/files- List available log files
Configuration
Log Directory Structure
loglensx expects logs in the following format:
logs/
├── log_file_2024-01-15.log
├── log_file_2024-01-14.log
└── log_file_2024-01-13.log
This works perfectly with the standard Python logging configuration:
import logging
handler = logging.FileHandler('logs/log_file_{}.log'.format(
datetime.now().strftime('%Y-%m-%d')
))
Custom Logger Configuration
from loglensx import LogParser
# Use custom log directory
parser = LogParser(log_dir="/var/log/myapp")
# Use custom regex pattern
custom_pattern = r'(?P<timestamp>.*?)\|(?P<level>\w+)\|(?P<message>.*)'
parser = LogParser(log_dir="logs", pattern=custom_pattern)
Advanced Usage
Filter Logs
from loglensx import LogAnalyzer
analyzer = LogAnalyzer(parser)
# Filter by level
errors = analyzer.filter_logs(level="ERROR", limit=50)
# Filter by logger
db_logs = analyzer.filter_logs(logger="database", limit=50)
# Search term
api_logs = analyzer.filter_logs(search_term="API", limit=50)
Get Statistics
# Log level distribution
level_stats = analyzer.get_level_statistics()
# Output: {'ERROR': 5, 'WARNING': 12, 'INFO': 234, 'DEBUG': 1000}
# Top loggers
top_loggers = analyzer.get_top_loggers(limit=10)
# Output: [('database', 450), ('api', 320), ('cache', 180), ...]
# Error frequency
error_freq = analyzer.get_error_frequency(hours=24)
# Output: {'2024-01-15 10:00': 3, '2024-01-15 11:00': 1, ...}
Generate Visualizations
from loglens.visualizers import ChartGenerator, TableGenerator
# Generate charts (returns JSON for Plotly)
level_chart = ChartGenerator.plotly_level_distribution(level_stats)
error_timeline = ChartGenerator.plotly_error_timeline(error_freq)
top_loggers_chart = ChartGenerator.plotly_top_loggers(top_loggers)
# Generate HTML tables
html_table = TableGenerator.logs_to_html_table(errors, title="Recent Errors")
Performance Considerations
- Large Log Files: loglensx efficiently handles large log files
- Real-time Parsing: Use
limitparameter to control parsing scope - Caching: Implement caching for frequently accessed data in production
Troubleshooting
Logs Not Appearing
- Check log directory exists:
logs/ - Verify log file naming:
log_file_YYYY-MM-DD.log - Check file permissions
- View server logs for parsing errors
Dashboard Not Loading
- Ensure FastAPI/Flask app is running
- Check URL prefix configuration
- Verify browser console for errors
- Check server logs for issues
Memory Issues with Large Logs
Use the limit parameter to control how many files are parsed:
parser.get_log_files(limit=7) # Only parse last 7 days
analyzer.get_log_summary() # Uses limited logs
Examples
See the examples/ directory for complete working examples:
fastapi_example.py- FastAPI integration with LogLensflask_example.py- Flask integration with LogLensstandalone_example.py- Standalone usage without frameworks
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
For issues, questions, or suggestions, please open an issue on GitHub.
Made with ❤️ for Python developers
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loglensx-1.0.1.tar.gz.
File metadata
- Download URL: loglensx-1.0.1.tar.gz
- Upload date:
- Size: 36.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ea138377fa7ffe6f8a3d5d1a3b1761ab29d03827df1e8cc97db6eb6d60c9a07
|
|
| MD5 |
45cc5f7e521cd7bbf57fc56b941d9004
|
|
| BLAKE2b-256 |
a81e276718aad7ce4176e7647b4fc51a001d91a7808b24889cebf2cf2a60ddae
|
File details
Details for the file loglensx-1.0.1-py3-none-any.whl.
File metadata
- Download URL: loglensx-1.0.1-py3-none-any.whl
- Upload date:
- Size: 19.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
304efd1b083aa1516ccff7d6b52c723ccf8bd19e40f779d01c7e66b2638bcc88
|
|
| MD5 |
f187e1dda3f4c5288f406865865a8fea
|
|
| BLAKE2b-256 |
efa948d1a51b7de23940fb341f462afc4564965625a686733bbaa7c8c7619876
|