An Isolation Forest-based anomaly detector for nginx access logs. It groups requests into one-minute windows, counts requests and 5xx errors per window, flags unusual minutes, and serves the results through a FastAPI service.
The project includes synthetic log generation with injected error spikes, so you can check that the detector catches them.
Live demo: https://log-anomaly-detector-with-fastapi-service.onrender.com/anomalies (also /chart)
The live demo runs on Render's free tier, which sleeps after a period of inactivity. The first request may take 30 to 60 seconds while the service starts. Later requests respond quickly.
- Parse:
parser.pyreads each nginx access log line into timestamp, method, path, status, and size. - Aggregate:
features.pycounts total requests, 5xx errors, and the error rate for each minute. - Detect:
detect.pytrains an Isolation Forest on those per-minute features and flags the outliers. - Serve:
main.pyexposes the results through a FastAPI service.plot.pydraws the error-rate chart with anomalies marked.
log-anomaly-detector/
├── generate_logs.py # creates a synthetic nginx log with known error spikes
├── parser.py # parses log lines into structured records
├── features.py # per-minute request and 5xx counts
├── detect.py # Isolation Forest anomaly detection
├── plot.py # saves the error-rate chart
├── main.py # FastAPI app
├── test_project.py # pytest test suite
├── locustfile.py # Locust load test
├── pytest.ini # pytest configuration
├── requirements.txt
├── Dockerfile
└── .gitignore
pip install -r requirements.txt
python generate_logs.py
uvicorn main:app --reloadThen open:
http://127.0.0.1:8000/anomaliesfor flagged minutes as JSONhttp://127.0.0.1:8000/chartfor the error-rate chart
docker build -t log-anomaly-detector .
docker run -p 8000:8000 log-anomaly-detectorThe Dockerfile generates the synthetic log during the build, so the image runs without any extra files.
| Endpoint | Description |
|---|---|
GET / |
Health check and list of endpoints |
GET /anomalies |
Flagged minutes as JSON, sorted by anomaly score |
GET /chart |
Error-rate chart with anomalies marked, as PNG |
Example response from /anomalies:
{
"total_minutes": 240,
"flagged_count": 12,
"anomalies": [
{
"minute": "2026-11-04T11:01:00+00:00",
"requests": 50,
"errors_5xx": 39,
"error_rate": 0.78,
"score": -0.1289
}
]
}The generator injects two spikes: 11:00 to 11:04 at a 60% 5xx rate, and 12:30 to 12:32 at 80%. The detector flags all 8 spike minutes, with 4 false alarms on normal traffic.
7 tests cover the parser, per-minute feature counting, the detector, and the API endpoints (/ and /anomalies). All pass.
pip install pytest httpx
python -m pytest -vRun against the local server (uvicorn main:app), using the same laptop for the server and the load generator. Each row is one 1-minute run.
| Users | Wait between requests | Requests | Failures | Median | 95th % | Throughput |
|---|---|---|---|---|---|---|
| 50 | 1 to 3 s | 1,412 | 0 | 9 ms | 25 ms | ~24 req/s |
| 200 | 1 to 3 s | 5,563 | 0 | 8 ms | 27 ms | ~93 req/s |
| 500 | none | 41,196 | 0 | 450 ms | 1,800 ms | ~690 req/s |
The 50 and 200 user runs are client-limited: the wait time caps throughput, so they don't show the server's limit. The 500 user run has no wait time, and throughput levels off around 690 req/s while latency rises, which marks where the system saturates on this hardware. Locust shares the laptop's CPU, so the true server limit may be higher.
# default (1 to 3 second wait)
python -m locust -f locustfile.py --headless -u 200 -r 20 -t 1m --host http://127.0.0.1:8000
# no wait, to find the ceiling (PowerShell)
$env:LOCUST_WAIT="0"
python -m locust -f locustfile.py --headless -u 500 -r 50 -t 1m --host http://127.0.0.1:8000Python, pandas, scikit-learn, matplotlib, FastAPI, Uvicorn, Docker, pytest, Locust.