From bd2eff2b95f6edd3572c05045b25738c7a12ce49 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Wed, 9 Sep 2026 17:33:14 +0200 Subject: [PATCH] =?UTF-8?q?chore(maafw):=20=E6=8C=89=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E4=BF=9D=E7=95=99=E5=A4=A9=E6=95=B0=E6=B8=85?= =?UTF-8?q?=E7=90=86=20MFW=20=E5=8E=9F=E7=94=9F=E6=97=A5=E5=BF=97=E5=A4=87?= =?UTF-8?q?=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MaaFramework 把 debug/maafw.log 写到一定大小就整体挪成 debug/maafw.bak.<时间戳>.log 再开新的,但从不回收旧的,一个每天跑的项目 几天就能堆出几百 MB。每次运行的完整内容已经另存进历史记录的 *.maafw.log, 原地那些备份是纯冗余。 - 新增 clean_maafw_native_debug_logs(),随启动清理一并执行,判据沿用 Function/HistoryRetentionTime,设为「永久」时整轮弃权 - 只删 maafw.bak.*.log,正在写的 maafw.log 与其他调试文件不动 Co-Authored-By: Claude Opus 5 --- CHANGELOG.md | 1 + app/core/config.py | 41 +++++++++++++++++++++++++++++++++++++++++ main.py | 1 + res/version.json | 3 ++- 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eec98b078..c84ce29c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,7 @@ - 代码清理 单源化各脚本专项重复的任务报告推送核心与 QQ/微信通知公共助手,并清理前后端死代码与直通包装组件,行为保持不变 by [@1w1w11w1](https://github.com/1w1w11w1) - MAA专项 代理接管 MAA 配置时强制开启开始唤醒的账号切换开关,确保按用户配置的账号切号;用户未填写账号时仍不会切号 by [@1w1w11w1](https://github.com/1w1w11w1) - MFW专项 进入项目配置页不再每次都要等一遍运行环境确认,开启自动更新时环境准备也提前到任务开始之前完成 by [@qiyinxi](https://github.com/qiyinxi) +- 日志清理 MFW 项目里 MaaFramework 轮转出的原生日志备份改为按历史记录的保留天数一并清理,不再无限堆积占用磁盘 ### 移除 diff --git a/app/core/config.py b/app/core/config.py index 226ee1989..a14227f75 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -4290,6 +4290,47 @@ async def clean_debug_diagnostics(self) -> None: if deleted_count: logger.success(f"清理完成: {deleted_count} 个过期诊断文件") + async def clean_maafw_native_debug_logs(self) -> None: + """清掉 MFW 项目里过期的 MaaFramework 原生日志备份。 + + MaaFramework 把 ``debug/maafw.log`` 写到一定大小就整体挪成 + ``debug/maafw.bak.<时间戳>.log`` 再开新的,但从不回收旧的——一个每天跑 + 的项目几天就能堆出几百 MB。每次运行的完整内容已经另存进历史记录的 + ``*.maafw.log``,所以这里只删备份,正在写的 ``maafw.log`` 不动。 + 保留时长沿用历史记录的保留天数设置。 + """ + + if self.get("Function", "HistoryRetentionTime") == 0: + logger.info("原生日志永久保留, 跳过 MFW 原生日志备份清理") + return + + from app.models.config import MaaFWConfig + + cutoff = time.time() - self.get("Function", "HistoryRetentionTime") * 86400 + deleted_count = 0 + for script_config in self.ScriptConfig.values(): + if not isinstance(script_config, MaaFWConfig): + continue + project_path = str(script_config.get("Info", "Path") or "").strip() + if not project_path: + continue + debug_folder = Path(project_path) / "debug" + if not debug_folder.is_dir(): + continue + # 备份文件名由 MaaFramework 决定,与 runner_task 里 + # _iter_rotated_native_debug_logs 认的是同一套。 + for file in debug_folder.glob("maafw.bak.*.log"): + try: + if file.stat().st_mtime >= cutoff: + continue + file.unlink() + except OSError as exc: + logger.warning(f"MFW 原生日志备份清理失败: {file} - {exc}") + continue + deleted_count += 1 + if deleted_count: + logger.success(f"清理完成: {deleted_count} 个过期 MFW 原生日志备份") + async def clean_old_history(self): """删除超过用户设定天数的历史记录文件(基于目录日期)""" diff --git a/main.py b/main.py index 42d6a8bf6..99df4cea6 100644 --- a/main.py +++ b/main.py @@ -284,6 +284,7 @@ async def initialize_background_services() -> None: await Config.clean_old_history() await Config.clean_maafw_agent_venvs() await Config.clean_debug_diagnostics() + await Config.clean_maafw_native_debug_logs() if IS_WINDOWS: for adapter in ("app.MaaFW.ArknightWin32",): diff --git a/res/version.json b/res/version.json index f0030c26a..45bdefa6c 100644 --- a/res/version.json +++ b/res/version.json @@ -64,7 +64,8 @@ "日志清理 自动清理OCR图片 by [@HarcoChen](https://github.com/HarcoChen)", "代码清理 单源化各脚本专项重复的任务报告推送核心与 QQ/微信通知公共助手,并清理前后端死代码与直通包装组件,行为保持不变 by [@1w1w11w1](https://github.com/1w1w11w1)", "MAA专项 代理接管 MAA 配置时强制开启开始唤醒的账号切换开关,确保按用户配置的账号切号;用户未填写账号时仍不会切号 by [@1w1w11w1](https://github.com/1w1w11w1)", - "MFW专项 进入项目配置页不再每次都要等一遍运行环境确认,开启自动更新时环境准备也提前到任务开始之前完成 by [@qiyinxi](https://github.com/qiyinxi)" + "MFW专项 进入项目配置页不再每次都要等一遍运行环境确认,开启自动更新时环境准备也提前到任务开始之前完成 by [@qiyinxi](https://github.com/qiyinxi)", + "日志清理 MFW 项目里 MaaFramework 轮转出的原生日志备份改为按历史记录的保留天数一并清理,不再无限堆积占用磁盘" ], "移除": [ "移除米游币获取任务,保留米游社各游戏签到。 by [@Lance0174](https://github.com/Lance0174)"