from django.conf import settings from snippet 638 import StandardExceptionMiddleware, _get_traceback class FileExceptionLoggerMiddleware(StandardExceptionMiddleware): def log_exception(self, request, exception, exc_info): from django.conf import settings log = open(settings.ERROR_LOG_FILE, 'w') from datetime import datetime log.write('--- Exception ---\n') log.write('Date: %s\n' % datetime.now()) log.write('IP: %s\n' % request.META.get('REMOTE_ADDR')) try: request_repr = repr(request) except: request_repr = "Request repr() unavailable" log.write('Request: %s\n' % request_repr) log.write('Traceback: %s\n' % _get_traceback(exc_info)) log.write('--- End ---\n\n') log.close()