A MODPYTHON Apache Log Handler
This module provides a logging Handler class to a MODPYTHON Apache server. Python's standard logging API explains how to use a handler.
The handler, by default, writes entries to the Apache error_log using the standard Python logging API.
VIRTUAL HOSTS (Python 2.5)
This handler also supports Apache Virtual Hosts where the mp_server object is available. Then, it writes entries to the specific virtual-host server's error_log.
To get the mp_server object out of Django, you need the log_extras() function in your logging call (See the source comments).
This module must be bound to the Python logging API. Use a site_logging.py module to do that as this related example shows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | """
A MODPYTHON Apache Log Handler
This module provides a logging Handler class to a MODPYTHON Apache server.
The handler writes entries to the Apache error_log using the standard
Python logging API.
EXAMPLE
This logs Exception object e to the Apache error_log at the WARNING level.
log.warning(e.message)
VIRTUAL HOSTS (Python 2.5)
This handler supports Apache Virtual Hosts where the mp_server object is available.
Then, it writes entries to the specific virtual-host server's error_log.
The mp_server object is passed in the extra dictionary as the 'server' key
This extra argument is a Python 2.5 feature.
VIRTUAL HOSTS EXAMPLE
This logs Exception object e to the current virtual-host's Apache error_log
at the ERROR level.
Pure MODPYTHON:
log.error(e.message, extra={ 'server': req.server })
django MODPYTHON:
log.error(e.message, extra=log_extras(request))
LOG LEVEL LIMITATIONS
Apache servers usually accept only log-level WARNING and above.
This handler filters log-levels to suit a common Apache build.
See the Python logging API module for more information.
(C) 2008 Andrew Droffner
License: PSF
"""
from mod_python import apache
import logging
class ApacheLogHandler(logging.Handler):
"""A handler class which sends all logging to Apache."""
def __init__(self, level=logging.NOTSET):
logging.Handler.__init__(self, level)
"""Map logging levels to Apache codes."""
self.level_mapping = {}
self.level_mapping[logging.ERROR] = apache.APLOG_ERR
self.level_mapping[logging.WARNING] = apache.APLOG_WARNING
self.level_mapping[logging.INFO] = apache.APLOG_INFO
self.level_mapping[logging.DEBUG] = apache.APLOG_DEBUG
def apache_level(self, record):
"""Map current record's logging level to Apache code."""
try:
if record.levelno:
return self.level_mapping[record.levelno]
else:
return self.level_mapping[self.level]
except (AttributeError, KeyError):
return apache.APLOG_ERR
def emit(self, record):
level = self.apache_level(record)
"""
Set MODPYTHON mp_server object so that vhost logs to its own error_log.
"""
try:
server = record.__dict__['server']
apache.log_error(record.getMessage(), level, server)
except KeyError:
apache.log_error(record.getMessage(), level)
"""
Django Virtual Host Support
Use the Django HttpRequest object to detect the virtual-host.
"""
def django_mp_vhost(request):
"""
Extract the MODPYTHON mp_server object from a Django HttpRequest object.
This depends on the Django/MODPYTHON *internals*, which no user should see!
"""
return request._req.server
def log_extras(request):
"""
logging extras function
This is a public logging extras function.
Users export and call log_extras(request) to get the current virtual-host.
"""
return { 'server': django_mp_vhost(request) }
|
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, 3 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, 7 months ago
Comments
Please login first before commenting.