问题描述
我想将特定级别和更高级别(例如INFO
及更高)的特定记录器名称的消息记录到特定的日志处理程序(例如文件处理程序),同时仍将所有日志消息发送到控制台. Python是2.7版.
I want to log messages of a specific logger name, of a certain level and higher (say INFO
and up) to a specific log handler, say a file handler, while still getting all log messages to the console. Python is version 2.7.
到目前为止,我一直尝试创建两个记录器:
What I tried until now was to create two loggers:
- 根记录器
- 命名记录器
对于root记录器,我附加了logging.StreamHandler
,并将日志级别设置为logging.DEBUG
.
For the root logger, I attached a logging.StreamHandler
, and set the log level to logging.DEBUG
.
然后,我将处理程序附加到命名记录器,并将该记录器的级别设置为logging.INFO
.
Then I attached a handler to the named logger and set level to logging.INFO
for that logger.
现在我调用使用命名记录器的模块时,不再将DEBUG
日志传播到根记录器.
When I now call my module, which uses the named logger, I do not get DEBUG
logs propagated to the root logger any more.
注意:extraLogger在此处有一个 StreamHandler 来演示该问题.在生产代码中,我将使用 FileHandler
Note: the extraLogger has a StreamHandler here to demonstrate the issue. In my production code I'd use a FileHandler
import logging
def do_logging(turn):
logger = logging.getLogger('extra')
logger.info('some info turn %d' % turn)
logger.debug('this is debug fudge turn %d' % turn)
rootLogger = logging.getLogger()
handler = logging.StreamHandler()
rootFormatter = logging.Formatter('root - %(levelname)s: %(msg)s')
handler.setFormatter(rootFormatter)
rootLogger.addHandler(handler)
rootLogger.setLevel(logging.DEBUG)
do_logging(1)
extraLogger = logging.getLogger('extra')
extraHandler = logging.StreamHandler()
extraFormatter = logging.Formatter('extra - %(levelname)s: %(msg)s')
extraHandler.setFormatter(extraFormatter)
extraLogger.addHandler(extraHandler)
extraLogger.setLevel(logging.INFO)
do_logging(2)
实际输出:
root - INFO: some info turn 1
root - DEBUG: this is debug fudge turn 1
extra - INFO: some info turn 2
root - INFO: some info turn 2
我想要的输出:
root - INFO: some info turn 1
root - DEBUG: this is debug fudge turn 1
extra - INFO: some info turn 2
root - INFO: some info turn 2
root - DEBUG: this is debug fudge turn 2
我怀疑自定义Filter
在这种情况下会有所帮助,但我不知道如何...
I suspect that a custom Filter
would be helpful in this case, but I do not know how...
推荐答案
您可以这样使用 robert的LevelFilter :
# Put the Filter on the Handler so only INFO and higher is handled
extraHandler.addFilter(LevelFilter(logging.INFO))
# Let the Logger process everything (so it can propagate records to root)
extraLogger.setLevel(logging.DEBUG)
import logging
class LevelFilter(logging.Filter):
"""
https://stackoverflow.com/a/7447596/190597 (robert)
"""
def __init__(self, level):
self.level = level
def filter(self, record):
return record.levelno >= self.level
def do_logging(turn):
logger = logging.getLogger('extra')
logger.info('some info turn %d' % turn)
logger.debug('this is debug fudge turn %d' % turn)
rootLogger = logging.getLogger()
handler = logging.StreamHandler()
rootFormatter = logging.Formatter('root - %(levelname)s: %(msg)s')
handler.setFormatter(rootFormatter)
rootLogger.addHandler(handler)
rootLogger.setLevel(logging.DEBUG)
do_logging(1)
extraLogger = logging.getLogger('extra')
extraHandler = logging.StreamHandler()
extraFormatter = logging.Formatter('extra - %(levelname)s: %(msg)s')
extraHandler.setFormatter(extraFormatter)
extraLogger.addHandler(extraHandler)
# Put the Filter on the Handler so only INFO and higher is handled
extraHandler.addFilter(LevelFilter(logging.INFO))
# Handle everything (so it can propagate to root)
extraLogger.setLevel(logging.DEBUG)
do_logging(2)
这篇关于Python日志记录:传播低于当前记录器级别的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!