【发布时间】:2021-11-15 19:24:21
【问题描述】:
我正在处理与 python logging specific level only 和 logging with filters 相同的问题,唯一的例外是我使用的是 .yaml 文件。
我查看了documentation,上面写着:
fileConfig() API 比 dictConfig() API 更老,并且没有 提供涵盖日志记录某些方面的功能。为了 例如,您不能配置过滤器对象,这些对象提供 过滤超出简单整数级别的消息,使用 文件配置()。如果您需要在日志记录中包含过滤器实例 配置,您将需要使用 dictConfig()。请注意,未来 配置功能的增强将被添加到 dictConfig(),所以值得考虑过渡到这个新的 方便时使用 API。
我检查了install filter on logging level in python using dictConfig 和Where is a complete example of logging.config.dictConfig?
我最新的 python 文件是这样的:
import logging
import logging.config
import yaml
with open('logging.yaml', 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
logger = logging.getLogger('module1')
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
logger.error('error message')
logger.critical('critical message')
logging.yaml 文件如下所示:
version: 1
formatters:
simple:
format: '{asctime} : {name} : {levelname} : {message}'
style: '{'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
module1:
level: DEBUG
handlers: [console]
propogate: no
root:
level: DEBUG
handlers: [console]
不明白如何修改 logging.yaml 文件,以便我只能在控制台中查看 INFO 并将 CRITICAL、ERROR、WARNING 保存到 logfile.log。
【问题讨论】: