python config_error ai_generated true

日志消息未被 caplog.records 捕获

Log message not captured in caplog.records

ID: python/pytest-capture-logging-not-working

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-11-05首次发现

版本兼容性

版本状态引入弃用备注
3.9 active

根因分析

日志记录器未配置为使用 pytest 的捕获处理器,通常因为日志器名称错误或传播设置问题。

English

日志记录器未配置为使用 pytest 的捕获处理器,通常因为日志器名称错误或传播设置问题。

generic

解决方案

  1. 90% 成功率 确保日志器名称正确
    logger = logging.getLogger('my_module')
    caplog.set_level(logging.INFO)
    logger.info('test')
  2. 80% 成功率 配置文件配置
    在 conftest.py 中配置 logging 插件,确保传播开启

无效尝试

常见但无效的做法:

  1. 在测试内配置 basicConfig 60% 失败

    尝试在测试中调用 logging.basicConfig,但 pytest 已经配置了处理器

  2. 错误地使用 __name__ 50% 失败

    使用 logging.getLogger(__name__) 但测试模块名称与预期不符