我正在使用命令行Python运行以下代码:
import logging
rootLog = logging.getLogger(__name__)
rootLog.setLevel(logging.INFO)
rootLog.warning("This is a root warning")
rootLog.info("This is root info")
def info():
log = rootLog.getChild("info")
log.info("This is info")
log.warning("This is a warning")
info()
我希望在控制台上看到所有四个日志消息,但是我只看到了警告。发生了什么?我有什么误解吗?
EDIT:
通过在脚本开头添加 logging.basicConfig(),我发现可以得到预期的输出。这很奇怪,因为 Python 的日志记录文档中指出:
“如果未为根记录器定义处理程序,则函数 debug()、info()、warning()、error() 和 critical() 将自动调用 basicConfig()。”