Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(log): shorten only the complete project root in messages

Rudy Celekli6bf8685f83eb
diff --git a/app/utils/logging_utils.py b/app/utils/logging_utils.py
index 03f37e54..a1290c8c 100644
--- a/app/utils/logging_utils.py
+++ b/app/utils/logging_utils.py
@@ -1,5 +1,6 @@
import functools
import os
+import re
import threading
from loguru import logger
@@ -8,6 +9,7 @@ from loguru import logger
PROJECT_ROOT = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
)
+_PROJECT_ROOT_IN_MESSAGE_RE = re.compile(re.escape(PROJECT_ROOT) + r"(?![\w.\-])")
LOG_RECORD_FORMAT = (
"<green>{time:%Y-%m-%d %H:%M:%S}</> | "
"<level>{level}</> | "
@@ -132,7 +134,9 @@ def format_log_record(record):
# 日志消息有时会包含任务文件的绝对路径。统一缩短为项目相对路径,可以
# 避免 WebUI 和终端因初始化入口不同而展示两套内容。
- record["message"] = record["message"].replace(PROJECT_ROOT, ".")
+ # 只替换完整的项目根路径;`<root>-backup`、`<root>2` 这类同级目录只是
+ # 前缀相同,按子串替换会把它们改成 `.-backup`、`.2` 这样错误的路径。
+ record["message"] = _PROJECT_ROOT_IN_MESSAGE_RE.sub(".", record["message"])
return LOG_RECORD_FORMAT
diff --git a/test/services/test_webui_task.py b/test/services/test_webui_task.py
index 7696d0d2..58f39522 100644
--- a/test/services/test_webui_task.py
+++ b/test/services/test_webui_task.py
@@ -677,6 +677,30 @@ def test_log_paths_stay_posix_style_on_every_platform():
assert record["file"].path == "./app/services/task.py"
+def test_log_messages_shorten_only_the_complete_project_root():
+ """
+ 同级目录(如 ``<root>-backup``、``<root>2``)只是与项目根路径前缀相同,
+ 不能被改写成 ``.-backup`` 这类错误路径;项目内路径仍应缩短。
+ """
+ root = logging_utils.PROJECT_ROOT
+ record = _log_record(
+ os.path.join(root, "app", "services", "task.py"),
+ message=(
+ f"saved {root}{os.sep}storage{os.sep}a.mp4, "
+ f"backup {root}-backup{os.sep}final.mp4, copy {root}2{os.sep}b.mp4, "
+ f"root {root}"
+ ),
+ )
+
+ logging_utils.format_log_record(record)
+
+ assert record["message"] == (
+ f"saved .{os.sep}storage{os.sep}a.mp4, "
+ f"backup {root}-backup{os.sep}final.mp4, copy {root}2{os.sep}b.mp4, "
+ "root ."
+ )
+
+
def test_log_paths_on_another_mount_do_not_discard_the_record():
"""
映射盘或 ``subst`` 盘启动时不能让整条日志消失。