You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何避免Python标准日志输出自动上传至Allure报告?

解决Allure报告自动添加Python logging输出为“log”附件的问题

以下是几种可行的解决方案:

1. 禁用Allure的标准输出/错误捕获

直接在运行pytest时添加--allure-no-capture参数,阻止Allure自动将stdout/stderr内容作为log附件加入报告:

pytest --alluredir=allure-results --allure-no-capture

如果需要长期生效,可以在项目的pytest.ini配置文件中添加该参数:

[pytest]
addopts = --alluredir=allure-results --allure-no-capture

2. 调整Python logging的输出目标

修改logging配置,让日志只输出到文件而非控制台(stdout),这样Allure就不会捕获到相关内容:

import logging
from logging.handlers import FileHandler

# 初始化logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.propagate = False  # 避免日志传递到根logger输出到stdout

# 添加文件处理器,将日志写入文件
file_handler = FileHandler('test_execution.log')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

# 此时日志只会写入文件,不会出现在控制台
logger.info('测试日志信息')

3. 事后移除已生成的log附件(不推荐)

如果已经生成了Allure结果文件,可以手动清理:

  • 打开allure-results目录下的测试结果JSON文件
  • 删除attachments数组中name为"log"的条目
  • 删除对应目录下的同名log附件文件

这种方法需要手动操作,适合临时应急,不如前两种方法高效。

内容的提问来源于stack exchange,提问作者Alexander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:52:06