什麼是使python的日誌記錄模塊與Apache/modpython一起工作的標準方式?登錄mod_python/apache
我想調用mylog.warn('whatever'),並導致req.log_error()的調用,其中req是modpython請求。
有沒有簡單的方法來設置它?
什麼是使python的日誌記錄模塊與Apache/modpython一起工作的標準方式?登錄mod_python/apache
我想調用mylog.warn('whatever'),並導致req.log_error()的調用,其中req是modpython請求。
有沒有簡單的方法來設置它?
我從來沒有這樣做過,但看起來編寫logging.Handler
的子類應該不那麼難。像這樣的東西應該可以做到。我不能說我實際上已經嘗試過,因爲我目前沒有安裝mod_python,但你應該能夠在某處調用logging.root.addHandler(ApacheLogHandler())
並使其運行。因人而異。
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)
我們使用以下內容。它完全記錄在here。
我們使用logging.fileConfig和logging.ini文件。
每個模塊使用logger= logging.getLogger(__name__)
然後我們就可以在我們的應用程序做logger.error( 「等等等等等等」)。
它完美地工作。 logging.ini文件定義了日誌文件的位置,以便我們可以將其保存在Web應用程序的其他日誌文件中。