Login or Sign up

Django process_exception to log tracebacks and debug

Posted by: skyl on Sept. 16, 2010

Running with DEBUG=True can be painfully slow so I was switching back and forth when I needed to debug. I'm very happy with another solution; run the site without DEBUG, even in development and add this middleware to log the traceback. There are many benefits to this. Ever get a 500 on an ajax call? It's a little difficult to debug b/c django can't load a 500 page for you in that situation to see the traceback.

import logging, traceback
logger = logging.getLogger(__name__)

class ExceptionMiddleware(object):

  def process_exception(self, request, exception):
      logger.debug(traceback.format_exc())
      return

Add this class to your MIDDLEWARE_CLASSES tuple (I think first is best) and you will be happy.

Comments on This Post:

Please Login (or Sign Up) to leave a comment