gpt4 book ai didi

python - 在 Python 的 logging.conf 中设置日志级别

转载 作者:太空宇宙 更新时间:2023-11-04 00:42:28 25 4
gpt4 key购买 nike

随着项目的发展,我正努力让代码调试变得容易。我讨厌添加和删除打印或调试语句(为什么不把它们留在里面?)问题是输出变得难以阅读。我正在尝试能够在不同的文件和级别中设置日志记录,以便我可以通过 logging.conf 配置文件打开或关闭它。

这是我的代码:

import logging.config
logging.config.fileConfig('logging.conf')
if __name__ == "__main__":
one = logging.getLogger("oneLogger")
two = logging.getLogger("twoLogger")
one.debug("debug on one")
one.info("info on one")
one.warn("warn on one")
one.error("error on one")
one.critical("critical on one")
two.debug("debug on two")
two.info("info on two")
two.warn("warn on two")
two.error("error on two")
two.critical("critical on two")

这是我的 logging.conf 文件:

[loggers]
keys=root,oneLogger, twoLogger

[handlers]
keys=rootHandler, oneHandler, twoHandler

[formatters]
keys=rootFormatter,oneFormatter, twoFormatter

[logger_root]
level=DEBUG
handlers=rootHandler

[logger_oneLogger]
level=DEBUG
handlers=oneHandler
qualname=main
propagate=1

[logger_twoLogger]
level=CRITICAL
handlers=twoHandler
qualname=main
propagate=1

[handler_rootHandler]
class=StreamHandler
formatter=rootFormatter
args=(sys.stdout,)

[handler_oneHandler]
class=StreamHandler
formatter=oneFormatter
args=(sys.stdout,)

[handler_twoHandler]
class=StreamHandler
formatter=twoFormatter
args=(sys.stdout,)

[formatter_rootFormatter]
format=Root: %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

[formatter_oneFormatter]
format=One: %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

[formatter_twoFormatter]
format=Two: %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

我会期望这样的输出:

One: 2016-12-22 16:36:32,414 - one - DEBUG - debug on one
One: 2016-12-22 16:36:32,414 - one - INFO - info on one
One: 2016-12-22 16:36:32,415 - one - WARNING - warn on one
One: 2016-12-22 16:36:32,417 - one - ERROR - error on one
One: 2016-12-22 16:36:32,417 - one - CRITICAL - critical on one
Two: 2016-12-22 16:36:32,421 - two - CRITICAL - critical on two

相反,我得到了这个:

Root: 2016-12-22 16:36:32,414 - one - DEBUG - debug on one
Root: 2016-12-22 16:36:32,414 - one - INFO - info on one
Root: 2016-12-22 16:36:32,415 - one - WARNING - warn on one
Root: 2016-12-22 16:36:32,417 - one - ERROR - error on one
Root: 2016-12-22 16:36:32,417 - one - CRITICAL - critical on one
Root: 2016-12-22 16:36:32,418 - two - DEBUG - debug on two
Root: 2016-12-22 16:36:32,418 - two - INFO - info on two
Root: 2016-12-22 16:36:32,420 - two - WARNING - warn on two
Root: 2016-12-22 16:36:32,421 - two - ERROR - error on two
Root: 2016-12-22 16:36:32,421 - two - CRITICAL - critical on two

我原以为记录器二将仅限于关键日志,而记录器一将接受所有日志。相反,我惊讶地看到根记录器处理了所有日志。

我在做什么/假设错了什么?

最佳答案

明白了。似乎“qualname”是我要找的,而不是记录器本身的名称:

这是我的代码:

import logging.config
logging.config.fileConfig('logging.conf')
if __name__ == "__main__":
one = logging.getLogger("one.oneLogger")
one.debug("debug on one")
one.info("info on one")
one.warn("warn on one")
one.error("error on one")
one.critical("critical on one")
two = logging.getLogger("two")
two.debug("debug on two")
two.info("info on two")
two.warn("warn on two")
two.error("error on two")
two.critical("critical on two")
root = logging.getLogger()
root.debug("debug on root")
root.info("info on root")
root.warn("warn on root")
root.error("error on root")
root.critical("critical on root")

这是我的 logging.conf 文件:

[loggers]
keys:root,twoLogger,oneLogger

[handlers]
keys:rootHandler,oneHandler,twoHandler

[formatters]
keys:rootFormatter,oneFormatter,twoFormatter

[logger_root]
level:DEBUG
handlers:rootHandler

[logger_oneLogger]
level:WARN
handlers:oneHandler
qualname:one
propagate:0

[logger_twoLogger]
level:CRITICAL
handlers:twoHandler
qualname:two
propagate:0

[handler_rootHandler]
class:StreamHandler
formatter:rootFormatter
args:(sys.stdout,)

[handler_oneHandler]
class:StreamHandler
formatter:oneFormatter
args:(sys.stdout,)

[handler_twoHandler]
class:StreamHandler
formatter:twoFormatter
args:(sys.stdout,)

[formatter_rootFormatter]
format:Root: %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt:

[formatter_oneFormatter]
format:One: %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt:

[formatter_twoFormatter]
format:Two: %(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt:

这是我的输出:

One: 2016-12-24 12:46:14,244 - one.oneLogger - WARNING - warn on one
One: 2016-12-24 12:46:14,246 - one.oneLogger - ERROR - error on one
One: 2016-12-24 12:46:14,246 - one.oneLogger - CRITICAL - critical on one
Two: 2016-12-24 12:46:14,247 - two.twoLogger - CRITICAL - critical on two
Root: 2016-12-24 12:46:14,249 - root - DEBUG - debug on root
Root: 2016-12-24 12:46:14,249 - root - INFO - info on root
Root: 2016-12-24 12:46:14,250 - root - WARNING - warn on root
Root: 2016-12-24 12:46:14,250 - root - ERROR - error on root
Root: 2016-12-24 12:46:14,252 - root - CRITICAL - critical on root

关于python - 在 Python 的 logging.conf 中设置日志级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41291883/

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