diff --git a/douyin/core/fudai_modules/base_interface.py b/douyin/core/fudai_modules/base_interface.py index 9da6124..ce082ba 100644 --- a/douyin/core/fudai_modules/base_interface.py +++ b/douyin/core/fudai_modules/base_interface.py @@ -5,9 +5,14 @@ from __future__ import annotations +import logging from typing import Any, Protocol, Optional from abc import ABC +# 模块级 logger:当 _enhanced_log_kv 不可用时走 logging 而非 stdout, +# 避免日志记录路径在异常分支里降级为裸 print。 +_LOGGER = logging.getLogger("douyin.core.fudai_modules.base_interface") + class FudaiAnalyzerInterface(Protocol): """福袋分析器接口协议""" @@ -54,11 +59,14 @@ class BaseFudaiMixin: if hasattr(self, '_enhanced_log_kv'): self._enhanced_log_kv(*args, **fields) # type: ignore else: - # 兜底:使用标准print - print(f"[LOG] {args} {fields}") + # 兜底:走模块级 logger + _LOGGER.info("[LOG] %s %s", args, fields) except Exception: # 最后兜底 - print(f"[LOG] {args} {fields}") + try: + _LOGGER.info("[LOG] %s %s", args, fields) + except Exception: + pass def _safe_operation_call(self, operation_name: str, *args, **kwargs) -> Any: """安全的操作调用,避免依赖未定义的属性""" diff --git a/gui/gui_app.py b/gui/gui_app.py index ccbb401..5efa313 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -1,4 +1,5 @@ import json +import logging import os import queue import re @@ -58,6 +59,9 @@ except ImportError: warnings.filterwarnings("ignore", message=".*pin_memory.*", category=UserWarning) warnings.filterwarnings("ignore", message=".*accelerator.*", category=UserWarning) +# 模块级 logger:内部"日志写入失败 / pump_logs 异常"等错误统一走 logging +_LOGGER = logging.getLogger("gui.gui_app") + def format_gui_log_line(line: str) -> str: """将完整日志行转换为 GUI 显示用的精简版本。 @@ -155,7 +159,7 @@ def main(): self._log_dir, f"unified_{today}.log" ) if not self._write_error_warned: - print(f"[GUI日志] 配置读取失败,使用默认路径: {e}", file=sys.stderr) + _LOGGER.warning("[GUI日志] 配置读取失败,使用默认路径: %s", e) def write(self, line: str) -> None: """线程安全地写入日志行到文件""" @@ -206,19 +210,19 @@ def main(): except PermissionError as e: if not self._write_error_warned: - print( - f"[GUI日志] 权限错误,无法写入日志文件: {self._log_file_path}", - file=sys.stderr, + _LOGGER.error( + "[GUI日志] 权限错误,无法写入日志文件: %s | %s", + self._log_file_path, + e, ) - print(f"[GUI日志] 错误详情: {e}", file=sys.stderr) self._write_error_warned = True except Exception as e: if not self._write_error_warned: - print( - f"[GUI日志] 写入日志文件失败: {self._log_file_path}", - file=sys.stderr, + _LOGGER.error( + "[GUI日志] 写入日志文件失败: %s | %s", + self._log_file_path, + e, ) - print(f"[GUI日志] 错误详情: {e}", file=sys.stderr) self._write_error_warned = True # 创建日志写入器实例 @@ -4154,13 +4158,13 @@ def main(): # 不再自动滚动到底部,让用户自由控制滚动位置 changed = True except Exception as e: - # 如果插入日志失败,至少打印到控制台(调试用) - print(f"[GUI日志错误] 无法插入日志: {e}", file=sys.stderr) + # 如果插入日志失败,至少记录到 logger(调试用) + _LOGGER.error("[GUI日志错误] 无法插入日志: %s", e) except queue.Empty: pass except Exception as e: # 捕获其他异常,确保日志循环不会中断 - print(f"[GUI日志错误] pump_logs异常: {e}", file=sys.stderr) + _LOGGER.error("[GUI日志错误] pump_logs异常: %s", e) try: if changed and not runner.is_running() and status_var.get() == "运行中": @@ -4173,7 +4177,7 @@ def main(): root.after(100, pump_logs) # type: ignore[arg-type] except Exception as e: # 如果 root 已被销毁,停止日志循环 - print(f"[GUI日志错误] 无法调度下次日志更新: {e}", file=sys.stderr) + _LOGGER.warning("[GUI日志错误] 无法调度下次日志更新: %s", e) # 统计信息显示 stats_top = ttk.Frame(stats_tab)