Skip to content

🐛 修复日志定时清理并统一后台业务日志记录 - #1599

Merged
CodFrm merged 1 commit into
mainfrom
fix/log-auto-cleanup
Jul 17, 2026
Merged

CodFrm merged 1 commit into
mainfrom
fix/log-auto-cleanup

Conversation

@CodFrm

@CodFrm CodFrm commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Checklist / 检查清单

  • Fixes mentioned issues / 修复已提及的问题
  • Code reviewed by human / 代码通过人工检查
  • Changes tested / 已完成测试

Description / 描述

问题

Manifest V3 重构后,日志页面仍可配置“定时清理 n 天前的日志”,log_clean_cycle 配置和 LoggerDAO.deleteBefore() 也仍然存在,但原有定时触发逻辑没有迁移,导致该功能只保存配置而不会真正清理日志。

同时,Service Worker 中部分可供用户排查的业务异常仍通过 console.* 输出,无法在运行日志页面查询。

修复

  • 新增持久化的 cleanupLogs alarm:
    • 首次在 1 分钟后运行;
    • 此后每 12 小时检查一次;
    • 创建前先读取现有 alarm,避免 MV3 Service Worker 冷启动不断重置倒计时。
  • 由 LogService 读取日志保留天数并调用 LoggerDAO.deleteBefore() 删除截止时间之前的日志。
  • 将保留天数 0 定义为关闭自动清理,避免误删全部日志。
  • 日志页面清理天数输入框增加 min={0}。
  • 仅在实际删除了过期日志时记录清理数量,避免周期性产生“清理 0 条”的无效日志。
  • 使用结构化 Logger 记录日志清理失败。

后台业务日志整理

将以下明确属于业务流程的 console.* 迁移到项目 Logger:

  • 扩展版本检查、读取更新配置和打开更新日志页面;
  • 脚本安装页准备、脚本更新检查、静默更新和批量安装更新;
  • Runtime 内置脚本加载、value 更新广播、session storage 配置、userScripts world 配置及脚本注销;
  • 定期更新检查时间写入和订阅更新检查;
  • GM API 扩展域名权限检查。

日志使用 service、component、uuid、path、count 等结构化标签。不会记录脚本 value 内容、网络正文、Cookie、Header 或响应内容。批量 UUID 日志限制为前 20 个,避免单条日志过大。

以下底层输出有意继续使用 console:

  • Chrome callback 的 chrome.runtime.lastError;
  • Logger/DBWriter 自身写入失败;
  • Logger 初始化前的启动错误;
  • 高频网络/CORS 诊断;
  • 注入到页面上下文中的 console 提示。

Tests / 测试

  • pnpm exec vitest run src/app/service/service_worker/log.test.ts src/app/service/service_worker/script.test.ts src/app/service/service_worker/runtime.test.ts src/app/service/service_worker/regular_updatecheck.test.ts --reporter=dot
    • 4 个测试文件、93 个测试通过。
  • pnpm run typecheck
  • 相关文件 ESLint
  • Prettier 格式检查
  • git diff --check
  • pnpm test 全量测试通过

Screenshots / 截图

无明显 UI 变化,仅为现有日志清理天数输入框增加最小值约束。

@CodFrm CodFrm changed the title 修复日志定时清理失效 🐛 修复日志定时清理失效 Jul 17, 2026
@CodFrm
CodFrm force-pushed the fix/log-auto-cleanup branch from fe00985 to 1cc3344 Compare July 17, 2026 07:56
@CodFrm
CodFrm force-pushed the fix/log-auto-cleanup branch from 1cc3344 to 6343e99 Compare July 17, 2026 08:05
@CodFrm CodFrm changed the title 🐛 修复日志定时清理失效 修复日志定时清理并统一后台业务日志记录 Jul 17, 2026
@CodFrm CodFrm changed the title 修复日志定时清理并统一后台业务日志记录 🐛 修复日志定时清理并统一后台业务日志记录 Jul 17, 2026
@CodFrm
CodFrm merged commit f0d297c into main Jul 17, 2026
9 of 10 checks passed
@CodFrm
CodFrm deleted the fix/log-auto-cleanup branch July 17, 2026 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant