This is another example use of the exception middleware. It shows how to log exceptions to a file. Someone wanted to do this to avoid DOS-ing the email server in case of a silly error.
(untested.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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()
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
so ... the StandardExceptionMiddleware is not defined
#
ruserious, you need to download snippet 638 as well, and then change the "from snippet 638 import..." in this snippet, to whatever you called your file with snippet 638. For example:
from myproject.middleware.snippet_638 import ...
#
Please login first before commenting.