log_report.py in Python
Summarize an access log's statuses, paths, and bytes.
"""Summarize an access log: status codes, top paths, bytes served."""
import re
import sys
from collections import Counter
from pathlib import Path
LINE = re.compile(r'"(?:GET|POST) (?P<path>\S+)[^"]*" (?P<status>\d{3}) '
r'(?P<size>\d+)')
def parse(text):
statuses, paths, total = Counter(), Counter(), 0
for line in text.splitlines():
m = LINE.search(line)
if not m:
continue
statuses[m.group("status")] += 1
paths[m.group("path")] += 1
total += int(m.group("size"))
return statuses, paths, total
def main():
source = Path(sys.argv[1]) if len(sys.argv) > 1 else None
text = source.read_text() if source else sys.stdin.read()
statuses, paths, total = parse(text)
print("status codes:", dict(statuses))
print("top paths:")
for path, hits in paths.most_common(3):
print(f" {hits:4d} {path}")
print(f"{total:,} bytes served")
if __name__ == "__main__":
main()
How it works
- A compiled regex pulls path, status, and size.
Countertallies statuses and top paths.- Totals print a compact report.
Keywords and builtins used here
continuedefdictelseforifintlenmainparseprintreturn
The run, in numbers
- Lines
- 35
- Characters to type
- 866
- Tokens
- 251
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 137 seconds.
Step 2 of 3 in Encore, step 40 of 41 in Domain tools.