Skip to main content

Interactive log viewer with charts and visualizations for FastAPI and Flask

Project description

LogLens - Interactive Log Viewer

PyPI version Python 3.8+ License: MIT

A powerful, interactive log viewer and analyzer for Python web applications. LogLens 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 LogLens 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 LogLens from PyPI:

pip install loglens

With Framework Support

FastAPI:

pip install loglens[fastapi]

Flask:

pip install loglens[flask]

Development:

pip install loglens[dev]

Quick Start

FastAPI Integration

from fastapi import FastAPI
from loglens import setup_fastapi_loglens

app = FastAPI()

# Setup LogLens at your preferred URL prefix
setup_fastapi_loglens(app, log_dir="logs", prefix="/loglens")

# Now access the dashboard at http://localhost:8000/loglens/

Flask Integration

from flask import Flask
from loglens 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 loglens 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

LogLens 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? LogLens accepts regex patterns for custom parsing.

API Endpoints

When integrated with FastAPI or Flask, LogLens exposes these API endpoints:

Dashboard

  • GET /loglens/ - Main dashboard

API Endpoints

  • GET /loglens/api/logs?search=term&level=ERROR&limit=100 - Get filtered logs
  • GET /loglens/api/stats - Get statistics and summary
  • GET /loglens/api/search?q=query - Search logs
  • GET /loglens/api/files - List available log files

Configuration

Log Directory Structure

LogLens 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 loglens 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 loglens 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: LogLens efficiently handles large log files
  • Real-time Parsing: Use limit parameter to control parsing scope
  • Caching: Implement caching for frequently accessed data in production

Troubleshooting

Logs Not Appearing

  1. Check log directory exists: logs/
  2. Verify log file naming: log_file_YYYY-MM-DD.log
  3. Check file permissions
  4. View server logs for parsing errors

Dashboard Not Loading

  1. Ensure FastAPI/Flask app is running
  2. Check URL prefix configuration
  3. Verify browser console for errors
  4. 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 LogLens
  • flask_example.py - Flask integration with LogLens
  • standalone_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

loglensx-1.0.0.tar.gz (35.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

loglensx-1.0.0-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file loglensx-1.0.0.tar.gz.

File metadata

  • Download URL: loglensx-1.0.0.tar.gz
  • Upload date:
  • Size: 35.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for loglensx-1.0.0.tar.gz
Algorithm Hash digest
SHA256 141bd6fb8530d4285fd656e356d0bd7c9d5af923fbd2e42992f508ebdd87dae0
MD5 21f5b8081b69b0a5f22657d0c5b3d9c1
BLAKE2b-256 723e3518f7a21f8011f84f5ac0289700219b6db29edd133e0c179ffe66823d5b

See more details on using hashes here.

File details

Details for the file loglensx-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: loglensx-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for loglensx-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6bb3bed505a2076cb2eef01a50802fcbc99b64cf403b678418941a47507bcdb6
MD5 e6c87cc74d35fb62a6cec27e66f1a0e1
BLAKE2b-256 3f222e3e7d22e728c4889ae396f2188d37a4b2e6deff6db243f5449a8966a34d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page