gpt4 book ai didi

Python 日志记录 : propagate messages of level below current logger level

转载 作者:太空狗 更新时间:2023-10-29 21:50:14 29 4
gpt4 key购买 nike

我想将特定记录器名称、特定级别或更高级别(比如 INFO 及以上)的消息记录到特定的日志处理程序,比如文件处理程序,同时仍然获取所有日志消息到控制台。 Python 是 2.7 版。

到目前为止我尝试的是创建两个记录器:

  • 根记录器
  • 命名记录器

对于根记录器,我附加了一个logging.StreamHandler,并将日志级别设置为logging.DEBUG

然后我将一个处理程序附加到指定的记录器,并将该记录器的级别设置为 logging.INFO

当我现在调用使用命名记录器的模块时,我不再将 DEBUG 日志传播到根记录器。

注意:extraLogger 在这里有一个StreamHandler 来演示这个问题。在我的生产代码中,我会使用 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 在这种情况下会有所帮助,但我不知道如何...

最佳答案

你可以使用 robert's 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 日志记录 : propagate messages of level below current logger level,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18058817/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com