把 douyin/logging/douyin_logging.py 按职责拆成
douyin/logging/douyin_logging_pkg/ 子包:
- _common (21) 共享全局状态:_logging_configured / _logger_cache / _log_monitor
- handlers (206) 自定义 handler:RotatingDatedFileHandler / DatedFileHandler
- cleanup (282) 日志压缩与清理:compress_log_file / cleanup_old_logs / _load_*_policy
- monitor (337) 轮转监控:LogRotationMonitor / start/stop_log_monitor / get_log_stats
- config (652) 主配置入口:get_logger / configure_logging / log_function_call /
LogContext / get_caller_info / format_exception_info
原文件改为 46 行 re-export shim,15 个公共符号全部保留。
全局变量统一搬到 _common,子模块通过 ``from . import _common as _state``
访问 _state.X 形式(global 不能跨模块)。
子包名 douyin_logging_pkg 是为了避免与同目录下的 shim 文件
douyin_logging.py 冲突(同名包 vs 模块在 Python 中不能并存)。
回归:
- 6 个新文件 + shim compileall 通过
- pytest 353 passed / 1 skipped(含 unified_logger MDC 13 个用例)
- 旧 import 路径与新子包路径返回同一函数对象
- get_logger 缓存机制(第二次调用返回同一对象)正常工作
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
22 lines
663 B
Python
22 lines
663 B
Python
"""douyin_logging_pkg._common - 共享全局状态。
|
||
|
||
被 handlers / cleanup / config / monitor 子模块共用:
|
||
- _logging_configured:configure_logging 是否已调用过
|
||
- _logger_cache:get_logger 缓存
|
||
- _log_monitor:LogRotationMonitor 实例(懒初始化)
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from typing import Dict, Optional, TYPE_CHECKING
|
||
|
||
if TYPE_CHECKING:
|
||
from .monitor import LogRotationMonitor
|
||
|
||
# 全局日志配置
|
||
_logging_configured = False
|
||
_logger_cache: Dict[str, logging.Logger] = {}
|
||
|
||
# 日志监控器实例(懒启动 / 在 start_log_monitor 内创建)
|
||
_log_monitor: "Optional[LogRotationMonitor]" = None
|