gui/web_gui: 内部错误 print → _wg_log,保留启动横幅

- 新增模块级 _wg_log(msg, level):与 yanghao/screenshot 同模式
- 内部错误(日志写权限/失败、静态文件、logs search/get_logs_since)
  6 处 print → _wg_log,分级 WARNING/ERROR
- main() 内的 7 处 print 保留为 stdout:启动端口/URL/中断退出/帮助提示
  属于交互/CLI 输出而非日志事件,走 stdout 更直观

回归:149 .py 编译通过;test_filter_logging / check_web_gui_config_sync 通过。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-21 09:38:47 +08:00
1 parent 5a381698d8
commit a876d9ebb0
1 file changed
+37 -9
+37 -9
View File
@@ -23,6 +23,32 @@ BASE_DIR = path_config.PROJECT_ROOT
CONFIG_PATH = path_config.get_config_path()
WEBUI_DIR = path_config.get_webui_dir()
import logging as _logging
# 模块级 logger:内部错误统一走 logging。
# 启动横幅和中断消息保留 print(属于交互/CLI 输出,不是日志事件)。
_LOGGER = _logging.getLogger("gui.web_gui")
def _wg_log(msg: str, level: str = "INFO") -> None:
"""统一 web_gui 内部错误日志出口;失败兜底 stdout。"""
try:
lv = str(level or "INFO").upper()
if lv == "ERROR":
_LOGGER.error(str(msg))
elif lv == "WARNING":
_LOGGER.warning(str(msg))
elif lv == "DEBUG":
_LOGGER.debug(str(msg))
else:
_LOGGER.info(str(msg))
except Exception:
try:
print(str(msg))
except Exception:
pass
# CSRF token for POST request protection (local-only server)
_CSRF_TOKEN = secrets.token_hex(32)
@@ -219,14 +245,14 @@ class ProcRunner:
except PermissionError as e:
# 权限错误,只输出一次警告避免刷屏
if not self._log_permission_warned:
print(f"[WEB_GUI] 警告:日志文件写入权限错误: {self._log_file_path}")
print(f"[WEB_GUI] 错误详情: {e}")
_wg_log(f"[WEB_GUI] 警告:日志文件写入权限错误: {self._log_file_path}", level="WARNING")
_wg_log(f"[WEB_GUI] 错误详情: {e}", level="WARNING")
self._log_permission_warned = True
except Exception as e:
# 其他文件写失败不应影响主流程,但记录第一次错误以便调试
if not self._log_write_error_warned:
print(f"[WEB_GUI] 警告:日志文件写入失败: {self._log_file_path}")
print(f"[WEB_GUI] 错误详情: {e}")
_wg_log(f"[WEB_GUI] 警告:日志文件写入失败: {self._log_file_path}", level="WARNING")
_wg_log(f"[WEB_GUI] 错误详情: {e}", level="WARNING")
self._log_write_error_warned = True
def _inc(self, key: str, n: int = 1) -> None:
@@ -1728,7 +1754,7 @@ class Handler(BaseHTTPRequestHandler):
except FileNotFoundError:
self.send_error(HTTPStatus.NOT_FOUND)
except Exception as e:
print(f"[WEB_GUI] Error serving static file {rel_path}: {e}")
_wg_log(f"[WEB_GUI] Error serving static file {rel_path}: {e}", level="ERROR")
self.send_error(HTTPStatus.INTERNAL_SERVER_ERROR)
def do_GET(self):
@@ -1802,8 +1828,9 @@ class Handler(BaseHTTPRequestHandler):
except Exception as e:
import traceback
print(
f"[WEB_GUI] logs search error: {e}\n{traceback.format_exc()}"
_wg_log(
f"[WEB_GUI] logs search error: {e}\n{traceback.format_exc()}",
level="ERROR",
)
return self._send_json(
{"next": 0, "lines": [], "running": RUNNER.is_running()}
@@ -1828,8 +1855,9 @@ class Handler(BaseHTTPRequestHandler):
# 记录错误但不中断请求,返回空日志
import traceback
print(
f"[WEB_GUI] get_logs_since error: {e}\n{traceback.format_exc()}"
_wg_log(
f"[WEB_GUI] get_logs_since error: {e}\n{traceback.format_exc()}",
level="ERROR",
)
return self._send_json(
{"next": 0, "lines": [], "running": RUNNER.is_running()}