所以我从命令行 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()
我希望在控制台上看到所有四个日志消息,但我只看到警告。到底是怎么回事?我误会了什么吗?
编辑:
我发现通过在脚本开头添加 logging.basicConfig() 可以得到预期的输出。这很奇怪,因为关于日志记录状态的 python 文档:
The functions
debug()
,info()
,warning()
,error()
andcritical()
will callbasicConfig()
automatically if no handlers are defined for the root logger.
最佳答案
您从手册中引用的部分在 Module-Level Functions 下并且仅在模块功能时适用
logging.debug()
字面意思是调用。由于您正在使用 rootLog.info()
调用实例方法,因此不会为您调用 basicConfig,您可能正在与空记录器对话。那里的文档有点困惑。
使用 loggging.basicConfig()
一切正常。
关于python - 为什么我在 python 中收不到日志信息消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805349/