From d5c9936c88d445a6459b2a931a6c69e9b5449c63 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 21 May 2026 11:39:07 +0800 Subject: [PATCH] =?UTF-8?q?gui=5Fapp=20+=20base=5Finterface:=20=E5=86=85?= =?UTF-8?q?=E9=83=A8=E9=94=99=E8=AF=AF=20print=20=E2=86=92=20logger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gui/gui_app.py(8 处 sys.stderr print → _LOGGER): - 配置读取失败、日志文件权限/写入错误(warning/error 级) - pump_logs 内日志插入失败、循环异常、调度下次更新失败(error/warning 级) - 顶部 import logging + 模块级 _LOGGER douyin/core/fudai_modules/base_interface.py(2 处 print → _LOGGER): - BaseFudaiMixin._safe_log 在主类无 _enhanced_log_kv 时的兜底改为 logger.info - 顶部 import logging + 模块级 _LOGGER 保留为 stdout 的合理场景: - bin/run_cli.py 的 [TEST]/[YANGHAO-ONLY] CLI 输出 - bin/manage_logs.py 的 CLI 工具输出 - gui/webgui_launcher.py: GUI 同时把 log 输出到控制台 - douyin/logging/internal_loggers.py: logger 模块自身的初始化失败兜底 - douyin/logging/douyin_logging{,_wrapper}.py: 日志系统内部的格式化输出 回归:140 .py 编译通过;test_filter_logging / check_web_gui_config_sync 通过; gui.gui_app/web_gui/gui_core/base_interface 全部可加载,_LOGGER 名称正确。 Co-Authored-By: Claude Opus 4.7 --- douyin/core/fudai_modules/base_interface.py | 14 +++++++--- gui/gui_app.py | 30 ++++++++++++--------- 2 files changed, 28 insertions(+), 16 deletions(-) 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)