Registering in mod_python / apache

What is the standard way to create a python logging module with apache / modpython?

I want to call mylog.warn ("whatever") and get this result when calling req.log_error () where req is a modpython request.

Is there an easy way to fix this?

+1


source to share


2 answers


I've never done this, but it seems like writing a subclass logging.Handler

shouldn't be that hard. Something like this should do the trick. I can't say I've actually tried this since I don't have mod_python currently installed, but you should be able to call logging.root.addHandler(ApacheLogHandler())

somewhere and get it to work. YMMV.



import logging
import apache

class ApacheLogHandler(logging.Handler):
    LEVEL_MAP = {
        logging.DEBUG: apache.APLOG_DEBUG,
        logging.INFO: apache.APLOG_INFO,
        logging.WARNING: apache.APLOG_WARNING,
        logging.ERROR: apache.APLOG_ERR,
        logging.CRITICAL: apache.APLOG_CRIT,
        }
    def __init__(self, request=None):
        self.log_error = apache.log_error
        if request is not None:
            self.log_error = request.log_error
    def emit(self,record):
        apacheLevel = apache.APLOG_DEBUG
        if record.levelno in ApacheLogHandler.LEVEL_MAP:
            apacheLevel = ApacheLogHandler.LEVEL_MAP[record.levelno]
        self.log_error(record.getMessage(), apacheLevel)

      

+2


source


We use the following. It is fully documented here .

  • We are using logging.fileConfig with the logging.ini file.

  • Each module uses logger= logging.getLogger(__name__)

  • Then we can do logger.error ("blah blah blah") throughout our entire application.



It works great. The logging.ini file defines where the log file is located so that we can store it with other log files for our web application.

-1


source







All Articles