Why Python's logging package ignores some levels?
展览:
使用3.6.3 python中的日志模块时,存在一些误解的问题。首先,我用标准配置创建两个日志,没有名称和级别=30,这就是logging.warning。
正如文档所说,日志对象不需要处理程序,我将日志级别更改为10(logging.debug),当我要打印日志时,信息(级别=20),首先,在级别10(低于信息级别)中,信息不记录,最后,日志没有任何处理程序,但是https://docs.python.org/3/library/logging.html的文档?highlight=logging模块日志记录显示:
logging.basicConfig(**kwargs)? :
Does basic configuration for the logging system by creating a
StreamHandler with a default Formatter and adding it to the root
logger. The functions debug(), info(), warning(), error() and
critical() will call basicConfig() automatically if no handlers are
defined for the root logger.
号
所以,我的问题是,我做错了什么?为什么会这样?我不能实例化日志,将根的级别设置为log.set level(logging.info),并将log.info("poo")设置为这个问题之前的操作吗?.
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | Python 3.6.3 (default, Oct 31 2017, 11:19:55) Type 'copyright', 'credits' or 'license' for more information IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help. In [1]: import logging In [2]: log1 = logging.getLogger() In [3]: log2 = logging.getLogger() In [4]: log1.level Out[4]: 30 In [5]: log1.hasHandlers() Out[5]: False In [6]: log1.setLevel(10) ## logging.DEBUG == 10 In [7]: log1 Out[7]: <RootLogger root (DEBUG)> In [8]: log2 Out[8]: <RootLogger root (DEBUG)> In [9]: log1.info("asdad") In [10]: log2.info("asdad") In [11]: log1.hasHandlers() Out[11]: False In [12]: logging.basicConfig(level=20) # logging.INFO == 20 In [13]: log2 Out[13]: <RootLogger root (INFO)> In [14]: log2.info("ASDADA") INFO:root:ASDADA In [15]: log1.hasHandlers() Out[15]: True In [16]: log1.handlers == log2.handlers Out[16]: True In [17]: logging.basicConfig(level=10) # logging.DEBUG == 10 In [18]: log2.info("ASDADA") INFO:root:ASDADA In [19]: log1 Out[19]: <RootLogger root (INFO)> In [20]: log2 Out[20]: <RootLogger root (INFO)> In [21]: log1.level Out[21]: 20 |
更新时间:1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | In [1]: import logging In [2]: logging.getLogger().hasHandlers() Out[2]: False In [3]: logging.getLogger().level Out[3]: 30 In [4]: logging.getLogger().info("papa") In [5]: logging.getLogger().hasHandlers() Out[5]: False In [6]: logging.info("foo") In [7]: logging.getLogger().hasHandlers() Out[7]: True |
号
更新时间:2
1 2 3 4 5 6 7 8 9 10 11 12 | In [2]: import logging In [3]: log = logging.getLogger() In [4]: log.info("poo") In [5]: log.warning("poo") poo In [6]: log.setLevel(logging.INFO) In [7]: log.info("poo") |
我认为引用的文件不够清楚。当您单击Docs网页上列出的函数时,它们会链接到
1 2 3 4 5 6 7 | >>> import logging >>> logging.getLogger().hasHandlers() False >>> logging.warning("TEST") WARNING:root:TEST >>> logging.getLogger().hasHandlers() True |