简体   繁体   English

登录mod_python / apache

[英]logging in mod_python/apache

What is the standard way to make python's logging module work with apache/modpython? 使python的日志记录模块与apache / modpython一起工作的标准方法是什么?

I want to call mylog.warn('whatever') and have that result in a call to req.log_error() where req is the modpython request. 我想调用mylog.warn('whatever')并导致调用req.log_error(),其中req是modpython请求。

Is there an easy way to set this up? 有一个简单的方法来进行设置吗?

I've never done it, but it seems that writing a subclass of logging.Handler shouldn't be that hard. 我从来没有做过,但是写logging.Handler的子类似乎并不难。 Something like this should do the trick. 这样的事情应该可以解决问题。 I can't say that I have actually tried this since I don't have mod_python installed currently but you should be able to call logging.root.addHandler(ApacheLogHandler()) somewhere and get it to work out. 我不能说我实际上已经尝试过此操作,因为当前没有安装mod_python,但是您应该可以在某个地方调用logging.root.addHandler(ApacheLogHandler())并使其正常工作。 YMMV. 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)

We use the following. 我们使用以下内容。 It's documented completely here . 在此处完整记录。

  1. We use logging.fileConfig with a logging.ini file. 我们将logging.fileConfig与logging.ini文件一起使用。

  2. Each module uses logger= logging.getLogger(__name__) 每个模块使用logger= logging.getLogger(__name__)

  3. Then we can do logger.error("blah blah blah") throughout our application. 然后,我们可以在整个应用程序中执行logger.error(“ blah blah blah”)。

It works perfectly. 它运作完美。 The logging.ini file defines where the log file goes so that we can keep it with other log files for our web app. logging.ini文件定义了日志文件的去向,以便我们可以将其与Web应用程序的其他日志文件一起保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM