Django Exception Logging Middleware

January 26, 2011

Here's some middleware that will log any exceptions raised to a file on disk.

import traceback
import socket
import warnings
import md5
from datetime import datetime

class CrashLogMiddleware(object):
    def process_exception(self, request, exception):
        server_name = socket.gethostname()
        tb_text     = traceback.format_exc()
        class_name  = exception.__class__.__name__
        checksum    = md5.new(tb_text).hexdigest()

        try:
            lLogFilePath = "/tmp/AppLog.log"

            lLogFile = open(lLogFilePath, "a")
            lLogFile.write("==== " + str(datetime.now()) + "=====================================\n")
            lLogFile.write(getattr(exception, "message", "") + "\n")
            lLogFile.write("Url: %s\n" % request.build_absolute_uri())
            lLogFile.write("Server: %s\n" % server_name)
            lLogFile.write(tb_text + "\n")
            lLogFile.flush()
            lLogFile.close()
    except Exception, exc:
        warnings.warn(unicode(exc))

I put this in project/home/init.py in my django source, and then referred to it from settings.py adding:

 'project.home.CrashLogMiddleware',

at the top of MIDDLEWARE_CLASSES.