- Author:
- simon
- Posted:
- September 17, 2009
- Language:
- Python
- Version:
- 1.1
- Tags:
- middleware apache errors modwsgi exceptions
- Score:
- 3 (after 4 ratings)
Add this to your middleware to log errors to the Apache error log when running under mod_wsgi.
1 2 3 4 5 6 7 | import traceback
class WsgiLogErrors(object):
def process_exception(self, request, exception):
tb_text = traceback.format_exc()
url = request.build_absolute_uri()
request.META['wsgi.errors'].write(url + '\n' + str(tb_text) + '\n')
|
More like this
- "Magic Link" Management Command by webology 3 weeks, 4 days ago
- Closest ORM models to a latitude/longitude point by simonw 3 weeks, 4 days ago
- Log the time taken to execute each DB query by kennyx46 3 weeks, 5 days ago
- django database snippet by ItsRLuo 1 month ago
- Serialize a model instance by chriswedgwood 2 months ago
Comments
This WSGI middleware is not necessary to get mod_wsgi to use the Python logging module.
mod_wsgi (version 2.5) sends sys.stderr to the Apache2 virtual host's error_log. Therefore, one only needs to create a logging.StreamHandler() on the sys.stderr stream (the default).
Create a site_logging.py module to do this (see below). Then, just import site_logging into the settings.py on start-up.
After that, write logging statements in any module.
import logging
logging.warn('WSGI sends to the Apache2 error_log.')
MOD_WSGI site_logging.py
import logging
import sys
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
#
Please login first before commenting.