How to pipe Apache logs to a python script

I wanted to pipe the output of my Apache’s error logs to a Python script. To do this you need to set the a CustomLog directive in your Apache Config.

Edit /etc/apache2/sites-available/default and add the following line:

CustomLog "| /usr/bin/yourscript" combined

To read the input into Python you need to setup a non-blocking read from stdin.

#! /usr/bin/python
import sys
import select

while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
    line = sys.stdin.readline()

    if line:
        # Read a line
        exit()

    else:
        # End of file
        exit()
else:
    # Nothing to read
   exit()

Obviously, this script just exit’s at end point but you could put logic at each point to process the text as it is read, not read or eof by python.

Let me know if you have any improvements to this script.

Leave a Reply