[mod_python] apache log handler

Nic Ferrier nferrier at tapsellferrier.co.uk
Sun Oct 16 19:43:28 EDT 2005


Does mod_python have a standard interface between python logging and
Apache's logging? 

I can't see one included in the dist. I know lots of other people have
written one. I've written one.

If there isn't one why isn't there one?


Nic Ferrier


PS for what it's worth here's mine:

# A log handler for mod-python

from mod_python import apache
import logging


class ProxyLogThing:
    """A proxy for default Apache logging."""

    def __init__(self):
        # No need to do anything.

    def log_error(msg, lvl):
        apache.log_error(msg, lvl)


class ApacheLogHandler(logging.Handler):
    """A handler class which sends all logging to Apache."""

    def __init__(self, ref = None):
        """
        Initialize the handler (does nothing)
        """
        logging.Handler.__init__(self)

        if ref == None:
            self.ref = ProxyLogThing()
        else:
            self.ref = ref

        # Set up the thing
        self.level_mapping = { logging.CRITICAL: apache.APLOG.CRIT,
                               logging.ERROR: apache.APLOG_ERR,
                               logging.WARNING: apache.APLOG_WARNING,
                               logging.INFO: apache.APLOG_INFO,
                               logging.debug: apache.APLOG_DEBUG }

    def emit(self, record):
        """Emit a record."""
        self.ref.log_error(record.msg, record.lvl)

# End


More information about the Mod_python mailing list