Skip to main content

A tool for analyzing NGINX access logs

Project description

nx-logstats: NGINX Log Analysis Tool

A NGINX log analysis tool that parses a given log file (e.g., NGINX access logs), summarizes key metrics such as request volume, status code distribution, frequently accessed endpoints, outputs a clean report and number of requests per hour of the day to showcase the temporal traffic patterns.

Overview

nx-logstats is a command-line utility that parses NGINX access logs, calculates key metrics, and presents them in a readable format. It's designed to be straightforward to use while providing meaningful insights from your server logs. Output can be in JSON or simple text. It can be shown in the terminal or stored in a file such as results.json.

Key features:

  • Parse standard NGINX access log formats
  • Extract multiple metrics from log data:
    • HTTP status code distribution
    • Most frequently accessed endpoints
    • Request volume by hour
    • HTTP method distribution
    • Average response size
  • Output to terminal or file in text or JSON formats
  • Robust error handling for malformed log entries
  • Different Logging levels that can be configured with options -v for info level logs and -vv for debug level logs.
  • Configured with Github Workflow which publishes the artifact to PyPi repository so that it can be accessed by anyone globally, available at https://pypi.org/project/nx-logstats/ and https://github.com/manvirheer/nx-logstats/actions.

Installation

From PyPI (Not suitable for running tests but packaged for production usage)

pip install nx-logstats

From Source (Preferred for running tests)

git clone https://github.com/manvirheer/nx-logstats.git
cd nx-logstats

Suggestion - Create a Virtual Environment

For Windows

python -m venv venv
venv\Scripts\activate

For Linux/MacOS

python -m venv venv
source venv/bin/activate

For development and testing:

pip install -e ".[dev]"

Usage

Basic usage:

There is a sample_logs/ folder available in the repository which can be used to test the solution.

nx-logstats ./sample_logs/valid_simple_logs.txt
nx-logstats ./sample_logs/errors_small_dataset.txt
nx-logstats ./sample_logs/errors_small_dataset.txt --ignore-errors
nx-logstats ./sample_logs/errors_small_dataset.txt --ignore-errors -f json -o results.json
nx-logstats ./sample_logs/errors_small_dataset.txt -v
nx-logstats ./sample_logs/errors_small_dataset.txt -vv

Command Line Options

usage: nx-logstats [-h] [-o OUTPUT] [-f {text,json}] [-n TOP_N] [-v] [--ignore-errors] logfile

NGINX log file analyzer - extracts and reports on key metrics. Accepted format is: <ip> - - [<dd/Mon/YYYY:HH:MM:SS>] "<HTTP_METHOD> <path> HTTP/1.1" <status> <bytes>

positional arguments:
  logfile               Path to the NGINX access log file to analyze

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Path to write the output report (default: print to stdout)
  -f {text,json}, --format {text,json}
                        Output format (text or json) (default: text)
  -n TOP_N, --top-n TOP_N
                        Number of top endpoints to include in the report (default: 10)
  -v, --verbose         Increase verbosity: -v for INFO, -vv for DEBUG. Default shows only errors. (default: 0)
  --ignore-errors       Ignore malformed log lines and continue processing (default: False)

Sample Output

When using the text format, the output will look similar to:

┌─────────────────────── NGINX ACCESS LOG ANALYSIS REPORT ───────────────────────┐
└──────────────────── Generated at: 2025-April-06 12:34:56 ────────────────────┘
┌─────────────────────────── General Statistics ───────────────────────────┐
│ Total Requests:       1242                                               │
│ Average Response Size: 4231.76 bytes                                     │
│ Generated at:         2025-April-06 12:34:56                             │
└────────────────────────────────────────────────────────────────────────┘
┌─── HTTP Status Code Distribution ────┐  ┌───── HTTP Method Distribution ─────┐
│ Status │ Count    │ Percentage       │  │ Method │ Count    │ Percentage     │
│ 200    │ 1024     │ 82.4%            │  │ GET    │ 985      │ 79.3%          │
│ 404    │ 156      │ 12.6%            │  │ POST   │ 192      │ 15.5%          │
│ 500    │ 42       │ 3.4%             │  │ PUT    │ 38       │ 3.1%           │
│ 302    │ 20       │ 1.6%             │  │ DELETE │ 27       │ 2.2%           │
└──────────────────────────────────────┘  └────────────────────────────────────┘

...
...
...

Running Tests

Run the test suite with:

pytest

License

This project is licensed under the MIT License - see the LICENSE file for details.

Design Considerations

For the design considerations, review the Design.md

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

nx_logstats-0.1.2.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

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

nx_logstats-0.1.2-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file nx_logstats-0.1.2.tar.gz.

File metadata

  • Download URL: nx_logstats-0.1.2.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nx_logstats-0.1.2.tar.gz
Algorithm Hash digest
SHA256 53c2c18104cc2fac0d60138ae7c4462c48975767b384a52507a79b9bdf049b6a
MD5 ddd6e56f28fbc08967af77a6b2b4ca62
BLAKE2b-256 6fccb3cd0a3eb193e4548c47a877fbb35a6ecb8a44efaf7e249678f0d316ea16

See more details on using hashes here.

File details

Details for the file nx_logstats-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: nx_logstats-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nx_logstats-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dd2aa623cd8cdb11703bc13c590340dbf4170ccc0d9a34cf64330da696d23dd7
MD5 7681cd055a014644020c1bc7287170aa
BLAKE2b-256 c877943dd8464377574d5abc17a10fa63f45e5b47cf228dc5b1d25663a9f0674

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