Skip to main content

Lightweight CLI for analyzing Apache/Nginx server logs

Project description

Log Lens Logo

Log Lens ๐Ÿ”

Lightweight, fast CLI for analyzing Apache/Nginx server logs with auto-format detection, rich formatting, and JSON export.

PyPI version Tests Coverage Code style: black Python 3.12+


โœจ Features

  • โœ… Auto log format detection - Apache, generic, and more
  • ๐ŸŽฏ Status codes analysis - 200, 404, 500 breakdowns
  • ๐Ÿ” Top IPs, paths, & HTTP methods - Rich table visualization
  • ๐ŸŽจ Rich CLI output - Colors, tables, formatted display
  • ๐Ÿ“Š JSON export - For dashboards, tools, and pipelines
  • ๐Ÿงช Production ready - 95% test coverage, strict linting, CI/CD
  • โšก Zero dependencies in runtime

๐Ÿš€ Quick Start

Install

pip install log-lens

Or from source:

git clone https://github.com/KnowOneActual/log-lens.git
cd log-lens
pip install -e .

Basic Usage

# Analyze a log file
log-lens /path/to/access.log

# Export to JSON
log-lens access.log -e report.json

# Limit top IPs to 5
log-lens access.log --top-ips 5

# Help
log-lens --help

๐Ÿ“– Usage Examples

Apache Access Logs

log-lens /var/log/apache2/access.log

Output:

โœ… Analyzed /var/log/apache2/access.log: 1,234 lines
๐Ÿ“Š Found 1,234 entries
๐Ÿ“‹ Format: APACHE

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Status Codes  โ”‚      โ”‚    Top IPs     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค      โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 200 โ”‚    892   โ”‚      โ”‚ 192.168.1.100  โ”‚ 156
โ”‚ 404 โ”‚     45   โ”‚      โ”‚ 10.0.0.15      โ”‚  89
โ”‚ 500 โ”‚      8   โ”‚      โ”‚ 172.16.0.50    โ”‚  42
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    HTTP Methods    โ”‚     โ”‚   Top Paths       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค     โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ GET  โ”‚    945     โ”‚     โ”‚ /                 โ”‚ 234
โ”‚ POST โ”‚     87     โ”‚     โ”‚ /api/login        โ”‚  89
โ”‚ HEAD โ”‚    134     โ”‚     โ”‚ /assets/style.css โ”‚  67
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Export JSON for Dashboards

log-lens access.log -e metrics.json

cat metrics.json

Output:

{
  "format": "apache",
  "ips": {
    "192.168.1.100": 156,
    "10.0.0.15": 89
  },
  "status_codes": {
    "200": 892,
    "404": 45,
    "500": 8
  },
  "top_paths": {
    "/": 234,
    "/api/login": 89,
    "/assets/style.css": 67
  },
  "methods": {
    "GET": 945,
    "POST": 87,
    "HEAD": 134
  }
}

Limit Results

# Show only top 5 IPs
log-lens access.log --top-ips 5

๐Ÿ›  Development Setup

Prerequisites

  • Python 3.12+
  • Poetry
  • Git

Install Development Environment

git clone https://github.com/KnowOneActual/log-lens.git
cd log-lens

# Install with dev dependencies
pip install -e '.[dev]'

# Install pre-commit hooks (auto-formatting)
poetry run pre-commit install

Running Tests

# Run all tests
poetry run pytest tests/ -v

# Run with coverage
poetry run pytest tests/ -v --cov=log_lens

# Run specific test
poetry run pytest tests/test_cli.py::TestCliIntegration -v

Expected output (95% coverage):

========== 11 passed in 0.24s ==========
---------- coverage: platform darwin, python 3.12.2-final-0 ----------
Name                   Stmts   Miss  Cover
------------------------------------------
log_lens/__init__.py       4      0   100%
log_lens/cli.py           73      7    90%
log_lens/parser.py        52      0   100%
------------------------------------------
TOTAL                    129      7    95%

Code Quality

# Format code
poetry run black log_lens/ tests/

# Sort imports
poetry run isort log_lens/ tests/

# Lint
poetry run ruff check log_lens/ tests/

# Run ALL checks at once
poetry run pre-commit run --all-files

๐Ÿ“‹ Pre-Commit Checklist

Always run this before committing:

# Run tests
poetry run pytest tests/ -v --cov=log_lens

# Run all formatters & linters
poetry run pre-commit run --all-files

See PRE_COMMIT_CHECKLIST.md for detailed workflow.

If formatters modify files:

git add .
git commit -m "style: format code"
poetry run pre-commit run --all-files  # Re-run to verify

๐Ÿค Contributing

Contributions welcome! Please read CONTRIBUTING.md for:

  • Development workflow
  • Commit message format
  • Testing requirements
  • PR guidelines

Quick summary:

  1. Fork & clone
  2. pip install -e '.[dev]'
  3. poetry run pre-commit install
  4. Make changes + write tests
  5. poetry run pytest tests/ -v --cov=log_lens (must pass)
  6. poetry run pre-commit run --all-files (must pass)
  7. Commit & push
  8. Submit PR

๐Ÿ“š Documentation


๐Ÿ— Project Structure

log-lens/
โ”œโ”€โ”€ log_lens/              # Main package
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli.py            # CLI entry point
โ”‚   โ””โ”€โ”€ parser.py         # Log parsing logic
โ”œโ”€โ”€ tests/                # Test suite (11 tests, 95% coverage)
โ”‚   โ”œโ”€โ”€ test_cli.py
โ”‚   โ””โ”€โ”€ test_parser.py
โ”œโ”€โ”€ docs/                 # Documentation
โ”œโ”€โ”€ pyproject.toml        # Project metadata & dependencies
โ”œโ”€โ”€ poetry.lock           # Locked dependencies
โ”œโ”€โ”€ README.md             # This file
โ”œโ”€โ”€ CONTRIBUTING.md       # Contribution guidelines
โ”œโ”€โ”€ PRE_COMMIT_CHECKLIST.md  # Pre-commit testing
โ””โ”€โ”€ .pre-commit-config.yaml  # Auto-formatting hooks

๐Ÿ”ง Technical Stack

  • Language: Python 3.12+
  • Package Manager: Poetry
  • Testing: pytest + pytest-cov (95% coverage)
  • Code Quality:
    • Black (formatting)
    • isort (import sorting)
    • Ruff (linting)
    • pre-commit (hooks)
  • CLI: Rich library for beautiful output
  • CI/CD: GitHub Actions

๐Ÿ“Š Specifications

Supported Log Formats

  • Apache access logs
  • Generic syslog format
  • Custom logs with log levels

Parsed Data

  • Status codes - HTTP response codes (200, 404, 500, etc.)
  • IP addresses - Source IPs and frequency
  • HTTP methods - GET, POST, HEAD, etc.
  • Request paths - Top accessed endpoints
  • Log levels - DEBUG, INFO, WARNING, ERROR, CRITICAL

๐Ÿ“ˆ Performance

  • Speed: ~5000 lines/sec on typical hardware
  • Memory: Minimal (~10MB for 100k log lines)
  • Output: Rich tables with ANSI colors

๐Ÿ› Troubleshooting

"Log format not detected"

The file might be in an unsupported format. Check JSON_SCHEMA.md.

"Permission denied"

Ensure you have read permissions:

chmod +r /path/to/log/file

Tests failing?

See PRE_COMMIT_CHECKLIST.md for debugging.


๐Ÿ“œ License

MIT License - See LICENSE for details.


๐Ÿ™ Acknowledgments

Made with โค๏ธ using:


Questions? Open an issue or start a discussion.

Happy analyzing! ๐Ÿ”โœจ

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

log_lens-0.7.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

log_lens-0.7.1-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file log_lens-0.7.1.tar.gz.

File metadata

  • Download URL: log_lens-0.7.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/24.6.0

File hashes

Hashes for log_lens-0.7.1.tar.gz
Algorithm Hash digest
SHA256 fd4772070d606aeb96775c97ea43a2b2b94fb49bc0b214a878708234181a6881
MD5 662a6407dd149c269fcc63ea68b4d0a6
BLAKE2b-256 88bc514a1173efaac31f7175d3724ecee778c15c387b87051343bdea9e95ff62

See more details on using hashes here.

File details

Details for the file log_lens-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: log_lens-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.14.3 Darwin/24.6.0

File hashes

Hashes for log_lens-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ca3af0cd59917abd09535178d161630d2de652c62cd09de06cacb94cf4be54f8
MD5 cd975c0b63f402d8caa9d27ecc02f83f
BLAKE2b-256 9fe80d8e762af50b6efbb4c470382ff32ebbffdb1cfcb8e769f3d4bdcefd95a9

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