# LogCaptureError: 捕获日志输出失败: 'NoneType' 对象没有属性 'write'

- **ID:** `python/pytest-logging-capture-failure`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

pytest 中的日志捕获机制失败，因为记录器没有处理程序或处理程序的流为 None，通常在重新配置后发生。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Reset the logger configuration in conftest.py: `logging.basicConfig(handlers=[logging.StreamHandler()], force=True)`
   ```
2. **** (95% 成功率)
   ```
   Avoid modifying root logger handlers in test code; use pytest's built-in caplog fixture instead.
   ```

## 无效尝试

- **** — This removes all log output from test reports, making debugging harder. (70% 失败率)
- **** — This can interfere with the test's logging assertions and may not fix the root cause. (80% 失败率)
