Introduction
When a website slows down or fails, engineers turn to its logs. This analyzer reads log lines from standard input and produces the summary an engineer would want first, skipping lines it can't understand.
Problem statement
Objectives
- Parse structured text with named regular-expression groups
- Aggregate with Counter and defaultdict
- Compute percentiles
- Present a multi-section report
Features
- Request and message line formats
- Level and status-class counts
- Endpoint latency with average, p90 and max
- Requests-per-hour chart
- Top errors and malformed-line report
Concepts demonstrated
- Regular expressions
- Aggregation
- Percentiles
- Walrus operator
System architecture
Two compiled patterns, REQUEST and MESSAGE, are tried in turn for each line with the walrus operator. analyse fills Counters for levels, statuses and hours, a defaultdict of response times per endpoint, and a Counter of errors (5xx responses and ERROR messages). percentile uses the nearest-rank method. main prints each section.
Folder structure
Complete source code
main.py
What each file does
- main.py
- The log patterns, the analysis pass, the percentile helper and the report.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
2026-11-27 09:00:01 INFO GET /home 200 120ms 2026-11-27 09:00:05 INFO GET /products 200 340ms 2026-11-27 09:01:12 INFO POST /cart 201 210ms 2026-11-27 09:02:30 WARN GET /products 200 980ms 2026-11-27 09:03:00 DEBUG cache warmed in 85ms 2026-11-27 09:05:00 ERROR Database timeout 2026-11-27 09:05:01 ERROR GET /checkout 500 3000ms 2026-11-27 10:10:10 INFO GET /home 200 95ms 2026-11-27 10:11:00 INFO GET /login 302 40ms 2026-11-27 10:12:45 INFO GET /missing 404 15ms this line is not a log entry 2026-11-27 10:20:00 ERROR Database timeout 2026-11-27 10:20:02 ERROR POST /checkout 503 2500ms 2026-11-27 11:00:00 INFO GET /products 200 310ms 2026-11-27 11:01:00 INFO POST /checkout 200 800ms 2026-11-27 11:30:00 WARN Disk usage at 85% 27/11/2026 11:31 INFO wrong date format 2026-11-27 11:45:00 INFO GET /home 200 110ms
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - Lines 11 and 17 are reported as malformed.
- GET /checkout (3000 ms) and POST /checkout (average 1650 ms) are the slowest endpoints.
- 'Database timeout' is the most common error, appearing twice.
Common errors
Lines with extra spaces aren't matched
Strip lines first and keep patterns anchored but flexible about whitespace.
Percentiles are off by one
Compute the index on the sorted list as round(p / 100 * (n - 1)).
Possible improvements
- Accept log files as command-line arguments
- Output the report as JSON for dashboards
- Alert when the error rate passes a threshold
Additional challenges
- 1Detect bursts: more than 3 errors within one minute.
- 2Group endpoints with ids (/user/17, /user/42) into /user/{id}.
- 3Compare two log files and show which endpoints got slower.
Built and tested it yourself? Mark it to update your dashboard.