![](/img/trans.png)
[英]Python - how do I configure a child logger via a logging configuration file
[英]Python logging - how do I disable or set different logging level for some modules, using the logger configuration file?
我正在從配置文件中讀取日志記錄配置:
logging.config.fileConfig(fname=logConfigFilePath, disable_existing_loggers=False)
。
日志記錄配置盡可能基本:
[loggers]
keys=root
[logger_root]
level=DEBUG
handlers=consoleHandler
[handlers]
keys=consoleHandler
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=consoleFormatter
args=(sys.stdout,)
[formatters]
keys=consoleFormatter
[formatter_consoleFormatter]
format=%(asctime)s %(levelname)s %(filename)s-%(funcName)s %(message)s
我需要為某些特定模塊設置不同的日志記錄級別(例如WARNING
),例如:
DEBUG connectionpool.py-_new_conn Starting new HTTPS connection (1):...
我怎樣才能做到這一點,不是以編程方式,而是使用配置文件?
謝謝。
在配置中,您可以通過其qualname操作運行時記錄器。
例如,假設您的記錄器位於模塊ab
中,並且您正在使用logging.getLogger(__name__)
。
您將擴展您的 loggers 部分以包含新的記錄器配置名稱,以及為記錄器提供不同級別的新部分,並通過 qualname 將其鏈接到運行時記錄器
[loggers]
keys=root,changed_logger
[logger_changed_logger]
qualname=a.b
level=DEBUG
;inherit all handlers from parent, do not create any new ones
propagate=1
handlers=
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.