python - 为什么我在 python 中收不到日志信息消息?

标签 python logging command-line

所以我从命令行 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() and critical() will call basicConfig() 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/

相关文章:

python - 使用自定义距离评估 Matern 内核

python - Pygame - 如何从下到上逐渐填充对象?

command-line - Windows 批处理错误 : "' ping' is not recognized as an internal or external command operable program or batch file."

python - BeautifulSoup 搜索属性-值

logging - 如何使用 logback 创建 2 个不同的 ROOT 记录器?

scala - 在类路径中找不到或忽略 log4j.properties 文件

mysql - 更新记录并在mysql中保留以前的记录数据

Python执行windows cmd函数

windows - QProcess - 正在运行的进程,其路径包含空格

python - 如何在多个子图上设置相同的轴值?