在Python中的logging.conf中设置日志记录级别

时间:2016-12-22 21:27:12

标签: python logging

我正努力让我的项目越来越容易调试我的代码。我讨厌添加和删除打印或调试语句(为什么不将它们留在?)问题是输出变得混乱。我试图能够设置不同文件和级别的日志记录,以便我可以通过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

我原本期望记录器2仅限于CRITICAL日志,记录器可以接受所有日志。相反,我很惊讶地看到根记录器处理了所有日志。

我在做什么/假设错了?

1 个答案:

答案 0 :(得分:3)

知道了。似乎" 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