簡體   English   中英

為什么Python記錄器無法在logging.INFO級別顯示信息或調試消息?

[英]Why doesn't Python logger display info or debug message at level logging.INFO?

有人可以向我解釋為什么.info()和.debug()調用即使看起來好像也不顯示任何內容? 我覺得即使在通過了日志記錄模塊文檔之后,我仍然不了解一些非常基本的內容。

$  python                                    
Python 3.6.5 (default, Apr 25 2018, 14:23:58) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logger = logging.getLogger()
>>> logger.setLevel(logging.INFO)
>>> logger.warning('This is a warning. We should see it.')
This is a warning. We should see it.
>>> logger.debug('This is a debug message. We should not see it.')
>>> logger.info('This is an info message. We should... see it, right?')
>>> logger.setLevel(logging.DEBUG)
>>> logger.info('This is an info message. We should... see it, right?')
>>> logger.debug('Weird. So I guess we are not going to see this as well?')
>>> 

它正在記錄日志,只是不記錄到終端。 如果要查看輸出到終端的日志(類似於print ),則需要添加一個處理程序:

logger.addHandler(logging.StreamHandler())

這樣做之后,日志記錄應該按預期顯示在您的終端上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM