拆分 douyin_logging_wrapper (1080 → 6 个子模块 + 59 行 shim)
把 douyin/logging/douyin_logging_wrapper.py 的 LoggingWrapper 类(8 个
@staticmethod)按职责拆成 douyin/logging/douyin_logging_wrapper_pkg/ 子包:
- _common (76) 共享:colorama / 占位类 / 模块实例
- enhanced (380) enhanced_log_kv 主入口
- simple (216) _enhanced_log_kv_simple 简化版
- detailed (356) _enhanced_log_kv_detailed 详细版(最大头)
- misc (151) 5 个小型辅助方法(log_step_time / log_error_with_context /
log_performance_step / log_debug_info / get_performance_stats)
- wrapper (55) LoggingWrapper 类(薄壳,每个方法 1 行 forwarding)
策略:8 个方法全是 @staticmethod 第一参数 analyser,搬到 module-level 后
类方法变成 1 行 forwarding。类内部互调(enhanced_log_kv → simple/detailed,
log_error_with_context → enhanced_log_kv)改为直接 import module-level 函数。
外部 import 路径不变:
``from douyin.logging.douyin_logging_wrapper import LoggingWrapper`` 仍可用
(原文件改成 59 行 re-export shim,含 18 个共享符号 / 4 个占位类)。
回归:
- 7 个新文件 + shim compileall 通过
- pytest 353 passed / 1 skipped
- LoggingWrapper.get_performance_stats / module-level 函数 / Fore/Style 占位
全部能正常调用
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
85bd488f66
commit
4ab1b88fa0
8 files changed
+1356
-1077
No files matched your search
@@ -0,0 +1,151 @@
|
||||
"""douyin_logging_wrapper_pkg.misc - 5 个小型日志辅助方法。
|
||||
|
||||
- log_step_time 记录步骤耗时
|
||||
- log_error_with_context 记录带上下文的错误
|
||||
- log_performance_step 记录性能步骤
|
||||
- log_debug_info 记录调试信息
|
||||
- get_performance_stats 获取性能统计快照
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from ._common import (
|
||||
COLORAMA_AVAILABLE,
|
||||
Fore,
|
||||
LOGGING_MODULES_AVAILABLE,
|
||||
Style,
|
||||
_action_logger,
|
||||
_debug_logger,
|
||||
_error_logger,
|
||||
_performance_tracker,
|
||||
)
|
||||
|
||||
|
||||
from .enhanced import enhanced_log_kv as _enhanced_log_kv
|
||||
|
||||
|
||||
def log_step_time(
|
||||
analyser: Any,
|
||||
step_name: str,
|
||||
start_time: float,
|
||||
extra_info: Optional[Dict[str, Any]] = None,
|
||||
) -> None:
|
||||
"""记录某个子步骤的耗时(供检测模块调用,减少日志噪音)。
|
||||
|
||||
参数:
|
||||
analyser: 分析器对象
|
||||
step_name: 步骤名称
|
||||
start_time: 开始时间(时间戳)
|
||||
extra_info: 额外信息字典
|
||||
"""
|
||||
try:
|
||||
elapsed = time.time() - float(start_time or 0.0)
|
||||
except Exception:
|
||||
elapsed = 0.0
|
||||
fields: Dict[str, Any] = {"seconds": elapsed}
|
||||
if extra_info:
|
||||
for k, v in extra_info.items():
|
||||
fields[str(k)] = v
|
||||
try:
|
||||
analyser._log_kv("步骤耗时", step_name, **fields) # type: ignore[attr-defined]
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
|
||||
|
||||
def log_error_with_context(
|
||||
analyser: Any,
|
||||
exc: BaseException,
|
||||
context: Optional[Dict[str, Any]] = None,
|
||||
level: str = "ERROR",
|
||||
) -> None:
|
||||
"""使用错误日志模块记录异常及上下文信息"""
|
||||
if LOGGING_MODULES_AVAILABLE and _error_logger:
|
||||
try:
|
||||
_error_logger.log_exception(exc, context=context, level=level)
|
||||
except Exception:
|
||||
pass # 错误日志记录失败不应该影响主流程
|
||||
|
||||
# 同时记录到标准日志
|
||||
_enhanced_log_kv(
|
||||
analyser,
|
||||
"错误",
|
||||
f"{type(exc).__name__}: {exc}",
|
||||
level=level,
|
||||
exception_type=type(exc).__name__,
|
||||
exception_message=str(exc),
|
||||
context=context or {},
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
|
||||
def log_performance_step(
|
||||
analyser: Any,
|
||||
step_name: str,
|
||||
elapsed_seconds: float,
|
||||
details: Optional[Dict[str, Any]] = None,
|
||||
) -> None:
|
||||
"""记录性能步骤信息"""
|
||||
if LOGGING_MODULES_AVAILABLE and _performance_tracker:
|
||||
try:
|
||||
# 记录到性能追踪器
|
||||
_performance_tracker.end_timer(f"{step_name}-{int(time.time()*1000)}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 记录到行为日志
|
||||
if LOGGING_MODULES_AVAILABLE and _action_logger:
|
||||
try:
|
||||
_action_logger.log_step(step_name, elapsed=elapsed_seconds)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 记录到标准日志
|
||||
fields = {"elapsed_seconds": elapsed_seconds}
|
||||
if details:
|
||||
fields.update(details)
|
||||
|
||||
_enhanced_log_kv(
|
||||
analyser, "性能", f"步骤完成: {step_name}", **fields
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
|
||||
def log_debug_info(analyser: Any, message: str, **context: Any) -> None:
|
||||
"""记录调试信息"""
|
||||
if LOGGING_MODULES_AVAILABLE and _debug_logger:
|
||||
try:
|
||||
# 这里可以扩展为更详细的调试信息记录
|
||||
_debug_logger.log_variable("debug_message", message)
|
||||
for key, value in context.items():
|
||||
_debug_logger.log_variable(key, value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 记录到标准日志
|
||||
_enhanced_log_kv(
|
||||
analyser, "调试", message, level="DEBUG", **context
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
|
||||
def get_performance_stats() -> Dict[str, Any]:
|
||||
"""获取性能统计信息"""
|
||||
if LOGGING_MODULES_AVAILABLE and _performance_tracker:
|
||||
try:
|
||||
return _performance_tracker.get_statistics()
|
||||
except Exception:
|
||||
pass
|
||||
return {}
|
||||
Reference in new issue
Block a user