Auto Scrolling Apache Log Script

Here is a python script that will print new requests to your server. I'm sure there are a lot of these, but I couldn't find any when I searched.

Anyway, set logloc to the location of your log file, and if you want it to update more quickly or slower, change updatefrq to the amount of seconds you want it to update.

It currently won't display the very last line.

logfilepyss.png

Here it is:

#! /usr/bin/env python
 
logloc = "/var/log/apache2/access.log" # Location of log file. Can be relative
updatefrq = 60   # num of seconds between updating. Just sleep(updatefrq)
 
 
from time import sleep
 
while 1:
	try:
		pointer = open("pointer.txt",'r').read()
	except:
		pointer = 0
		open("pointer.txt","w").write(pointer)
	file = open(logloc,'r')
	file.seek(int(pointer))
	line = file.readline().replace("\n","")
	while line != "":
		print line
		line = file.readline().replace("\n","")
	open("pointer.txt","w").write("%d" % (file.tell(),))
	file.close()
 
	sleep(updatefrq)

Or download as a .txt file

4 Comments

  1. def ZA says:

    Um, does this do what tail does?

    tail -f /var/log/apache2/access.log

    updates whenever the file changes. AFAIK

    Cheers,

    David

  2. ah_skeet says:

    More or less, yes.

    There are some differences.

    Tail shows the last 10 lines whenever you start, while this shows however many new lines there were since the last display.

    Tail "monitors" the file, this just opens the file, goes to the pointer, and fires off whatever new lines are after.

    Tail updates instantly, while this updates every 60 seconds.

    Tail, I think, will only display the last 10 lines of every update. If you have a file that gets updated in batch, i.e. 50 lines a time like this server does, then you'll miss a lot of lines.

  3. def ZA says:

    You can specify to "follow" the file as you describe above like so:

    tail -f blah.txt

    the "-f" is follow, and updates as new stuff arrives in the file.

    tail blah.txt
    will show 10 lines by default once off.
    tail -n 50 blah.txt
    will do the same with 50 lines.

    tail -f -n 50 blah.txt
    Will follow the file after showing the first 50 lines.

  4. ah_skeet says:

    Right. I understand that it will follow it; however, I thought it would only show the last 10 lines after an update if the update consisted of 100s, but I just tried it, and it'll show all of them. My mistake on that part.

    This still doesn't only show only the unprinted lines from an execution before.

    So, if I kill tail -f -n 50 access.log to do something, like edit a file, and run tail -f -n 50 access.log again, that will still show me 50 lines, where most, if not all, would probably be old.

    If I killed python logscroll.py to do something else, and ran python logscroll.py again, it would show only the new lines since the time I killed it, whether that be 0 or 50.

    I could've just stopped them instead of killing them, but it would still hold true if I needed to restart, logout, or lost the connection.

    Running tail -f will probably be more than enough for most people, and I'm not saying this script is better. In fact, I'm pretty sure it's worse. It was just something I hacked together and thought that someone might find this useful.

Leave a Reply