Files
douyi_guaji_main/douyin/logging/douyin_logging_wrapper.py
T
Your Name 0b5f150691 Refactor and clean up code across multiple scripts
- Removed trailing whitespace and ensured files end with a single newline in cleanup_unused_files.py.
- Added fix_trailing_whitespace.py to batch remove trailing whitespace from Python files.
- Updated install_dependencies.py to improve dependency installation prompts.
- Cleaned up pick_coords.py by removing unnecessary whitespace and improving code readability.
- Enhanced region_selector.py by disabling specific pylint checks for better compatibility with third-party libraries.
- Improved WPS流程图生成工具.py by cleaning up whitespace and enhancing readability.
- Updated __init__.py in typings to remove unnecessary lines.
- Modified app.js to adjust default configuration values and clean up unused code.
2026-01-02 23:57:03 +08:00

705 lines
28 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
"""
日志管理模块
============
职责:日志记录和格式化
- 增强版日志记录
- 步骤时间记录
- 统一日志格式
"""
import sys
import os
import time
import re
import inspect
import traceback
from typing import Any, Dict, Optional
from datetime import datetime
# 导入所有日志模块
try:
from .error_logger import ErrorLogger # type: ignore[assignment]
from .debug_logger import DebugLogger # type: ignore[assignment]
from .action_logger import ActionLogger # type: ignore[assignment]
from .performance_tracker import PerformanceTracker # type: ignore[assignment]
LOGGING_MODULES_AVAILABLE = True
except ImportError:
# 如果导入失败,提供占位符类
class ErrorLogger:
def log_exception(self, *args, **kwargs): pass
class DebugLogger:
def log_variable(self, *args, **kwargs): pass
class ActionLogger:
def log_step(self, *args, **kwargs): pass
class PerformanceTracker:
def end_timer(self, *args, **kwargs): pass
def get_statistics(self, *args, **kwargs): return {}
LOGGING_MODULES_AVAILABLE = False
# 初始化日志模块实例
_error_logger = ErrorLogger()
_debug_logger = DebugLogger()
_action_logger = ActionLogger()
_performance_tracker = PerformanceTracker()
class LoggingWrapper:
"""日志包装器:统一管理日志功能"""
@staticmethod # type: ignore[misc]
def enhanced_log_kv(analyser: Any, *args: Any, **fields: Any) -> None:
"""增强的日志记录方法:始终输出到 stdout(供 GUI 捕获),同时可选输出到标准 logging
参数:
analyser: 分析器对象
*args: 位置参数,第一个是tag,第二个是msg
**fields: 关键字参数字典
"""
# 解析参数:优先使用位置参数,避免与关键字参数冲突
if len(args) >= 1:
tag = args[0]
# 如果位置参数提供了 tag,从 fields 中移除 tag 避免冲突
fields.pop("tag", None)
else:
tag = fields.get("tag", "日志")
if len(args) >= 2:
msg = args[1]
# 如果位置参数提供了 msg,从 fields 中移除 msg 避免冲突
fields.pop("msg", None)
else:
msg = fields.get("msg", "")
# 获取日志级别
log_level = fields.get("level", "INFO").upper()
if log_level not in ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"):
log_level = "INFO"
# 递增日志序号
try:
analyser._log_seq += 1
except Exception:
analyser._log_seq = 1
# 获取时间戳
try:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
except Exception:
# 如果无法导入 datetime(极少见),回退为 time.strftime
try:
timestamp = time.strftime("%H:%M:%S")
except Exception:
timestamp = ""
# 获取调用者信息(模块名:函数名:行号)
caller_info = ""
try:
# 获取调用栈:跳过 _enhanced_log_kv 本身,找到真正的调用者
frame = inspect.currentframe()
if frame:
# frame.f_back 是调用 _enhanced_log_kv 的帧(可能是 _log_kv 或其他)
# 需要再往上找一层,找到实际调用 _log_kv 的代码
caller_frame = frame.f_back
if caller_frame:
# 检查是否是 _log_kv 的调用(属性赋值的情况)
if (
caller_frame.f_code.co_name == "_log_kv"
or "log_kv" in caller_frame.f_code.co_name
):
caller_frame = caller_frame.f_back
# 如果找到了调用者,提取信息
if caller_frame:
filename = caller_frame.f_code.co_filename
funcname = caller_frame.f_code.co_name
lineno = caller_frame.f_lineno
# 只取文件名,不包含路径
filename = os.path.basename(filename)
# 如果文件名是 <string> 或类似,说明是测试代码,跳过
if filename and filename not in ("<string>", "<stdin>"):
caller_info = f"{filename}:{funcname}:{lineno}"
except Exception:
pass
# 字段映射(中文 key)
key_map: Dict[str, str] = {
"rid": "运行",
"seq": "序号",
"msg": "说明",
"stage": "阶段",
"seen": "识别",
"ok": "成功",
"text": "文本",
"action": "动作",
"result": "结果",
"mode": "模式",
"reason": "原因",
"wait_seconds": "等待秒",
"value_yuan": "价值元",
"future": "开奖时间",
"pos_seen": "发现位置",
"qsize": "队列数量",
"room_pos": "当前位置",
"content": "内容",
"countdown": "倒计时",
"participants": "参与人数",
"copies": "份数",
"seconds": "耗时秒",
"error": "错误",
"error_type": "错误类型",
"fudai_x": "福袋X坐标",
"attempt": "尝试次数",
"room_title": "直播间名",
"back_times": "需回退次数",
}
# 构建日志行(增强格式:时间 级别 标签 [调用者] 消息 字段...)
log_parts: list[str] = []
if timestamp:
log_parts.append(f"[{timestamp}]")
log_parts.append(f"[{log_level}]")
log_parts.append(f"[{tag}]")
# 添加调用者信息(如果获取成功)
if caller_info:
log_parts.append(f"[{caller_info}]")
if msg:
m = str(msg).replace("\r", " ").replace("\n", " ").strip()
if len(m) > 200:
m = m[:200] + "..."
log_parts.append(m)
# 添加字段信息
for k, v in fields.items():
if k in ("tag", "msg", "level", "exc_info", "_show_caller", "_caller"):
continue
if v is None:
continue
key_cn = key_map.get(str(k), f"字段_{k}")
# 格式化值
if isinstance(v, float):
if abs(v) < 1:
v_str = f"{v:.3f}"
elif abs(v) < 100:
v_str = f"{v:.2f}"
else:
v_str = f"{int(v)}"
elif isinstance(v, (int, bool)):
v_str = str(v)
else:
s = str(v).replace("\r", " ").replace("\n", " ").strip()
if len(s) > 100:
s = s[:100] + "..."
v_str = s
log_parts.append(f"{key_cn}={v_str}")
# 构建完整日志行
log_line = " ".join(log_parts)
# 判断是否需要添加分隔符(便于查看日志)
need_separator = False
separator_reason = ""
# 1. 错误发生时(优先级最高)
if log_level in ("ERROR", "WARNING", "CRITICAL"):
need_separator = True
separator_reason = "错误/警告"
# 2. 运行开始时
elif tag and any(
keyword in str(tag) for keyword in ["运行", "启动", "开始", "初始化"]
):
need_separator = True
separator_reason = "运行开始"
# 3. 阶段变化时(tag变化且包含阶段相关关键词)
elif tag and getattr(analyser, "_last_log_tag", None) != tag:
if any(
keyword in str(tag)
for keyword in ["阶段", "循环", "参与", "检测", "寻找", "进入", "退出"]
):
need_separator = True
separator_reason = "阶段变化"
# 4. 重要操作(通过tag判断)
elif tag and getattr(analyser, "_last_log_tag", None) != tag:
if any(
keyword in str(tag)
for keyword in ["参与", "中奖", "完成", "失败", "超时"]
):
need_separator = True
separator_reason = "重要操作"
# 输出分隔符(如果需要)
if need_separator:
try:
# 使用等号作为分隔符,更清晰易读
separator = "=" * 80
# 在分隔符中间显示原因,更美观
reason_text = f" {separator_reason} "
separator_line = (
f"{separator}\n{reason_text.center(80, '=')}\n{separator}"
)
print(separator_line, flush=True)
except Exception:
pass
# 更新上一个日志标签
analyser._last_log_tag = tag
# 始终输出到 stdout(供 GUI 捕获)- 这是最重要的!
try:
print(log_line, flush=True) # flush=True 确保立即输出,不缓冲
except Exception:
pass
# 写入行为日志(actions.log)- 使用运行日志格式,但只保留关键信息
# 记录所有操作到行为日志
try:
# 判断是否需要记录(记录所有操作,但可以过滤一些噪音日志)
should_log = True
# 过滤一些不需要记录的日志(避免噪音)
if tag:
# 跳过一些调试或详细日志
skip_tags = ["[调试图像]", "[步骤耗时]"]
for skip_tag in skip_tags:
if tag.startswith(skip_tag):
should_log = False
break
if should_log:
# 获取时间戳
if not timestamp:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
# 提取关键信息:做了什么、耗时、分数
# 如果msg为空,从tag中提取操作描述
if msg:
action_desc = str(msg)
# 移除函数名:匹配 "函数名:" 或 "函数名 " 格式
# 先移除带冒号的函数名
action_desc = re.sub(
r"^[a-zA-Z_][a-zA-Z0-9_]*\s*[::]\s*", "", action_desc
)
# 再移除带空格的函数名(但后面不是字母/下划线开头的单词)
action_desc = re.sub(
r"^[a-zA-Z_][a-zA-Z0-9_]*\s+(?=[^a-zA-Z_])", "", action_desc
)
# 如果移除后为空,保留原描述
if not action_desc.strip():
action_desc = str(msg)
# 简化描述:移除字段前缀,只保留关键信息
if "字段_" in action_desc:
# 提取阶段信息(如果有)
if "字段_阶段=" in action_desc:
stage_match = re.search(r"字段_阶段=(\w+)", action_desc)
if stage_match:
action_desc = f"阶段={stage_match.group(1)}"
else:
# 简化其他字段描述,只保留消息部分
action_desc = action_desc.split("字段_")[0].strip()
else:
# 从tag中提取操作类型(移除标签的方括号)
if tag:
tag_clean = tag.strip("[]").strip()
if tag_clean:
action_desc = tag_clean
else:
action_desc = "操作"
else:
action_desc = "操作"
# 提取耗时(从字段中查找,优先使用毫秒字段)
elapsed_str = ""
if "elapsed_ms" in fields:
elapsed_seconds = float(fields.get("elapsed_ms", 0)) / 1000.0
elapsed_str = f"耗时:{elapsed_seconds:.2f}s"
elif "total_elapsed_ms" in fields:
elapsed_seconds = float(fields.get("total_elapsed_ms", 0)) / 1000.0
elapsed_str = f"耗时:{elapsed_seconds:.2f}s"
elif "总耗时毫秒" in fields:
elapsed_seconds = float(fields.get("总耗时毫秒", 0)) / 1000.0
elapsed_str = f"耗时:{elapsed_seconds:.2f}s"
elif "seconds" in fields:
elapsed_seconds = float(fields.get("seconds", 0))
elapsed_str = f"耗时:{elapsed_seconds:.2f}s"
elif "elapsed_seconds" in fields:
elapsed_seconds = float(fields.get("elapsed_seconds", 0))
elapsed_str = f"耗时:{elapsed_seconds:.2f}s"
# 提取分数(如果有)
score_str = ""
if "score" in fields and fields.get("score") is not None:
try:
score_val = float(fields.get("score", 0))
score_str = f"分数:{score_val:.3f}"
except (ValueError, TypeError):
pass
elif "max_val" in fields and fields.get("max_val") is not None:
try:
score_val = float(fields.get("max_val", 0))
score_str = f"分数:{score_val:.3f}"
except (ValueError, TypeError):
pass
elif "分数" in fields:
score_field = fields.get("分数")
# 允许显示0.0分数,只要不是None
if score_field is not None:
try:
score_val = float(score_field)
score_str = f"分数:{score_val:.3f}"
except (ValueError, TypeError):
pass
# 构建精简的行为日志行,使用运行日志格式:时间 - [GUI] - [标签] 做了什么 耗时:XXs 分数:XX
action_parts = [timestamp, "-", "[GUI]", "-"]
if tag:
action_parts.append(tag)
else:
action_parts.append("[日志]")
action_parts.append(action_desc)
if elapsed_str:
action_parts.append(elapsed_str)
if score_str:
action_parts.append(score_str)
action_log_line = " ".join(action_parts)
# 写入 actions.log 文件
# 使用统一的路径配置
try:
import path_config
actions_log_path = path_config.get_actions_log_path()
log_dir = path_config.get_logs_dir()
except Exception:
# 回退到旧路径(向后兼容)
actions_log_path = os.path.join(
os.path.dirname(__file__), "logs", "actions.log"
)
log_dir = os.path.dirname(actions_log_path)
if log_dir:
os.makedirs(log_dir, exist_ok=True)
# 检测标签变化,用====分隔不同步骤
# 使用实例变量记录上一个标签
if not hasattr(analyser, "_last_action_tag"):
analyser._last_action_tag = None
# 如果标签变化,添加分隔符
current_tag = tag or "[日志]"
if (
analyser._last_action_tag is not None
and analyser._last_action_tag != current_tag
):
# 标签变化,添加分隔符(增加到80个等号)
with open(
actions_log_path, "a", encoding="utf-8-sig", errors="replace"
) as f:
f.write("=" * 80 + "\n")
f.flush()
# 更新上一个标签
analyser._last_action_tag = current_tag
# 写入日志行
with open(
actions_log_path, "a", encoding="utf-8-sig", errors="replace"
) as f:
f.write(action_log_line + "\n")
f.flush()
except Exception:
pass
# 同时输出到标准 logging(如果已配置,可选)
try:
# 确保日志系统已配置
import logging
root_logger = logging.getLogger()
if not root_logger.handlers:
# 如果根 logger 没有 handlers,尝试配置日志系统
try:
from douyin.logging import douyin_logging
douyin_logging.configure_logging()
except Exception:
pass
if not hasattr(analyser, "_std_logger") or analyser._std_logger is None:
try:
from unified_logger import get_unified_logger
analyser._std_logger = get_unified_logger("douyin.fudai")
except ImportError:
try:
from douyin.logging import douyin_logger
analyser._std_logger = douyin_logger.get_logger("douyin")
except Exception:
# 如果都失败了,使用根 logger
analyser._std_logger = logging.getLogger("douyin.fudai")
# 使用根 logger 确保日志能写入文件(子 logger 会传播到根 logger)
root_logger = logging.getLogger()
if root_logger.handlers:
# 如果根 logger 有 handlers,使用根 logger 写入(确保能写入文件)
# 构建标准 logging 消息(包含调用者信息)
std_msg_parts = [f"[{tag}]"]
if caller_info:
std_msg_parts.append(f"[调用:{caller_info}]")
if msg:
# 对消息进行敏感信息脱敏
try:
from unified_logger import SensitiveDataMasker
masked_msg = SensitiveDataMasker.mask_all(str(msg))
std_msg_parts.append(masked_msg)
except ImportError:
std_msg_parts.append(str(msg))
# 对字段进行敏感信息脱敏
masked_fields: Dict[str, Any] = {}
try:
from unified_logger import SensitiveDataMasker
masked_fields = SensitiveDataMasker.mask_dict(fields)
except ImportError:
masked_fields = fields
for k, v in masked_fields.items():
if k in (
"tag",
"msg",
"level",
"exc_info",
"_show_caller",
"_caller",
):
continue
if v is not None:
std_msg_parts.append(f"{k}={v}")
log_message = " ".join(std_msg_parts)
# 检查是否有异常信息
exc_info = fields.get("exc_info")
if exc_info is None:
if sys.exc_info()[0] is not None:
exc_info = sys.exc_info()
# 记录到根 logger(确保能写入文件)
# 优化:只写入文件,不输出到控制台(避免与 print 重复)
level = getattr(logging, log_level, logging.INFO)
# 临时禁用控制台输出,只写入文件
console_handlers = [h for h in root_logger.handlers if isinstance(h, logging.StreamHandler) and h.stream == sys.stdout]
for handler in console_handlers:
handler.setLevel(logging.CRITICAL + 1) # 临时禁用
try:
if exc_info:
root_logger.log(
level,
log_message,
exc_info=exc_info,
extra={"caller": caller_info},
)
else:
root_logger.log(level, log_message, extra={"caller": caller_info})
finally:
# 恢复控制台输出级别
for handler in console_handlers:
handler.setLevel(logging.NOTSET)
elif hasattr(analyser, "_std_logger") and analyser._std_logger:
# 如果根 logger 没有 handlers,尝试使用 _std_logger(但通常不会到这里)
# 优化:跳过标准 logging 输出,避免与控制台 print 重复
# 因为 print 已经输出了,这里只用于文件日志(如果需要)
pass # 不再输出到标准 logging,避免重复
except Exception as e:
# 如果标准 logging 写入失败,记录详细错误信息并尝试使用根 logger 写入(作为备用)
try:
if _error_logger:
_error_logger.log_exception(e, context={"tag": tag, "caller": caller_info})
except Exception:
pass
# 如果标准 logging 写入失败,继续使用根 logger 写入(作为备用)
try:
import logging
root_logger = logging.getLogger()
level = getattr(logging, log_level, logging.INFO)
# 构建简单的日志消息
simple_msg = f"[{tag}] {msg}" if msg else f"[{tag}]"
# 直接使用根 logger,它会自动处理子 logger 的传播
root_logger.log(level, simple_msg)
except Exception:
# 如果所有日志写入都失败,至少确保 stdout 输出(已在上面完成)
pass
# 如果有异常堆栈,也输出到 stdout
exc_info = fields.get("exc_info")
if exc_info is None:
if sys.exc_info()[0] is not None:
exc_info = sys.exc_info()
if exc_info:
try:
# exc_info 应该是 (type, value, traceback) 元组
if isinstance(exc_info, tuple) and len(exc_info) == 3:
exc_type, exc_value, exc_traceback = exc_info
# 确保 exc_value 是 BaseException 实例
if isinstance(exc_value, BaseException):
tb_str = traceback.format_exception(
exc_type, exc_value, exc_traceback
)
else:
# 如果不是异常实例,创建一个包装异常
try:
# 尝试使用原始值
tb_str = traceback.format_exception(
exc_type,
(
BaseException(str(exc_value))
if exc_value
else BaseException("Unknown error")
),
exc_traceback,
)
except Exception:
# 如果还是失败,只输出异常信息
print(
f" 异常类型: {exc_type}, 异常值: {exc_value}",
flush=True,
)
tb_str = []
for line in tb_str:
print(f" {line.rstrip()}", flush=True)
else:
# 如果不是标准格式,尝试直接格式化
try:
tb_str = traceback.format_exception(*exc_info)
for line in tb_str:
print(f" {line.rstrip()}", flush=True)
except Exception:
# 如果格式化失败,只输出异常信息
print(f" 异常信息: {exc_info}", flush=True)
except Exception:
pass
@staticmethod # type: ignore[misc]
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 # 错误日志记录失败不应该影响主流程
# 同时记录到标准日志
LoggingWrapper.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)
LoggingWrapper.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
# 记录到标准日志
LoggingWrapper.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 {}