简体   繁体   English

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

[英]Setting logging levels in logging.conf in Python

I am trying to make it easy to debug my code as my project grows. 我试图随着项目的增长轻松调试我的代码。 I hate adding and removing print or debug statements (why not just leave them in?) The problem is the output becomes messy to read. 我讨厌添加和删除打印或调试语句(为什么不将它们留在里面?)问题是输出变得混乱读取。 I am trying to be able to set up logging in different files and levels so I can turn it on or off via the logging.conf configuration file. 我试图能够在不同的文件和级别中设置日志记录,以便可以通过logging.conf配置文件将其打开或关闭。

Here is my code: 这是我的代码:

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")

Here is my logging.conf file: 这是我的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=

I would have expected this output: 我本来期望这样的输出:

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

Instead I got this: 相反,我得到了这个:

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

I was expecting that logger two would be limited to only CRITICAL logs and logger one would accept all logs. 我期望记录器2将仅限于CRITICAL日志,而记录器1将接受所有日志。 Instead, I was surprised to see that the root logger handled ALL of the logs. 相反,我很惊讶地看到根记录器处理了所有日志。

What am I doing/assuming wrong? 我在做什么/假设做错了?

Got it. 得到它了。 It seems that "qualname" is what I was looking for and NOT the name of the logger itself: 似乎“ qualname”是我想要的,而不是记录器本身的名称:

Here is my code: 这是我的代码:

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")

Here is my logging.conf file: 这是我的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:

Here is my output: 这是我的输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM