Sphere
Log in Join

A small Python script for tailing nginx logs with status filters

Our staging nginx log hits about 2 GB a week. For months I've been doing tail -f /var/log/nginx/access.log | grep ' 500 ' in one tmux pane and losing the earlier lines every time I scroll. I wanted something that kept the last few thousand lines and let me filter by status code without writing a grep incantation at 2am. So I wrote a 60-line Python script.

## What it does

logtail.py tails a file, keeps the last 10,000 lines in a deque, and prints only the lines whose status code matches a filter. Coloured output: 5xx red, 4xx yellow, 3xx cyan, 2xx plain. It re-reads from the last position every 500 ms, so it also works on a log that's being appended to, as long as the inode doesn't change.

## The script

#!/usr/bin/env python3
import re, sys, time
from collections import deque

STATUS = re.compile(r'" (\d{3}) ')
COLOURS = {"5": "\033[31m", "4": "\033[33m", "3": "\033[36m"}
RESET = "\033[0m"

def main(path, wanted):
buf = deque(maxlen=10000)
with open(path, encoding="utf-8", errors="replace") as fh:
fh.seek(0, 2) # start at EOF, like tail -f
while True:
line = fh.readline()
if not line:
time.sleep(0.5)
continue
m = STATUS.search(line)
code = m.group(1) if m else "000"
if wanted and not code.startswith(wanted):
continue
colour = COLOURS.get(code[0], "")
print(f"{colour}{line.rstrip()}{RESET}", flush=True)
buf.append(line)

if __name__ == "__main__":
main(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else "")

Python 3.11.8 on Debian 12, no dependencies. The deque(maxlen=10000) is doing the memory work, you don't need to manage eviction yourself. To be honest I never actually read buf in the version I run, I added it so I could dump the last N lines on a keypress and then never wired that up. I'll get to it.

There's no argparse setup, the second argument is just a prefix, so 5 matches 5xx and 50 would match 500-509 if you wanted to get that specific. Anything else falls through to everything. I kept it deliberately dumb because the only two things I ever type are 5 and 4.

## How I run it

python3 /usr/local/bin/logtail.py /var/log/nginx/access.log 5
python3 /usr/local/bin/logtail.py /var/log/nginx/access.log 4
python3 /usr/local/bin/logtail.py /var/log/nginx/access.log

First one shows server errors, second client errors, third everything. I put the first in a tmux pane called errs and leave it running. The regex assumes a standard combined log format where the status is preceded by a quote and a space, if your nginx log_format is custom, adjust STATUS.

## What it deliberately doesn't do

No parsing of user agents, no geo lookup, no aggregation. There are better tools for that, goaccess is what I use when I need actual numbers. This is just a faster version of the thing I was already doing with grep. It also doesn't handle logrotate creating a new file, when that happens I restart the pane. I could add inode watching but honestly it happens once a week and pressing Ctrl-C twice is fine.

The script is short enough that you can just paste the block above, no repo, no install step.