Here is a simple python logging utility that I made to help log cron and anacron jobs but it can be used with any command. The script simply prefaces the stdout and stderr of the command with a date and time stamp. This is very useful if you have, say, an rsync cron job that runs every day or week. It’s very similar to what the system log looks like but you don’t have to muck with the complicated syslogd.

#!/usr/bin/env python
#
# pylogger.py
# created: 2011-05-04 by brent @ http://intvoid.com
#
# requirements: python 2.6/2.7 or 3.0+
# Logs the output of a command (both stdout and stderr) and prefaces each line
# with a date and timestamp.  Very useful for cron jobs or recurring rsync
# operations where the output needs to be logged to a file over time.

# for compatibilty with python3's print function
from __future__ import print_function

import sys, datetime
import subprocess

cmd = sys.argv[0]
cmd_args = sys.argv[1:]

def logtime():
    # formate date in iso format and remove last 4 decimal places
    now = datetime.datetime.now().isoformat(" ")[:-4]
    return now + ":"

print(logtime(), "pylogger STARTED with args =", sys.argv)
print(logtime(), "----------BEGIN command output----------")

# not sure if bufsize=1 is need, but it won't hurt
proc = subprocess.Popen(cmd_args, stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT, bufsize=1)
proc.poll()
line = " "

while proc.returncode != 0 or line:
    line = proc.stdout.readline()
    if not line:
        break
    # needed to add decode() because I was getting b'string' output with python3
    # since it uses bytes instead of str type
    print(logtime(), line.rstrip().decode('utf8') )
    proc.poll()

print(logtime(), "----------END command output----------")
print(logtime(), "pylogger ENDED")
print(80*"-")

I’ve written this to be compatible with Python 3.0 which (if you’re not familiar with it) uses the print() function call in lieu of the built-in print from python.  This basically means that a previous

print 'spamalot'

needs to be incorporated in parentheses like this

print('spamalot')

More info can be gleaned from here:  http://docs.python.org/release/3.0.1/whatsnew/3.0.html

To use the script all you have to do is preface your command with “pylogger.py”. So running the following command (note that I have put pylogger.py in my $PATH which includes /home/brent/bin):

$ pylogger.py ps -f

will yield the following output:

2011-05-25 00:26:32.05: pylogger STARTED with args = ['/home/brent/bin/pylogger.py', 'ps', '-f']
2011-05-25 00:26:32.05: ----------BEGIN command output----------
2011-05-25 00:26:32.09: UID        PID  PPID  C STIME TTY          TIME CMD
2011-05-25 00:26:32.09: brent     1943  1904  0 May24 pts/0    00:00:00 bash
2011-05-25 00:26:32.09: brent    11420  1943  0 00:26 pts/0    00:00:00 python /home/brent/bin/pylogger.py ps -f
2011-05-25 00:26:32.09: brent    11421 11420  0 00:26 pts/0    00:00:00 ps -f
2011-05-25 00:26:32.09: ----------END command output----------
2011-05-25 00:26:32.09: pylogger ENDED
--------------------------------------------------------------------------------

I’ve used and tested this script on ubuntu linux and on mac osx with multiple versions of python (2.6, 2.7, 3.0, 3.1) without any problems so if you have any let me know.

« »