Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(tray): tray.log 不收 httpx 每轮健康检查的 INFO

longmeidao66ead4a8405e
diff --git a/src/peach/tray.py b/src/peach/tray.py
index 72597a0c3..a46c0282a 100644
--- a/src/peach/tray.py
+++ b/src/peach/tray.py
@@ -1392,7 +1392,8 @@ def log_to_file(path: Path) -> logging.Handler | None:
"""托盘自己的日志另落一份文件,返回挂上的 handler;写不了返回 None。
pythonw 下没有 stderr,`basicConfig` 那一份无处可落,启停时序出问题时只剩进程创建
- 时间可对。写不了只是少一份证据,不拦托盘启动。
+ 时间可对。写不了只是少一份证据,不拦托盘启动。httpx 每轮健康检查各记一条 INFO,
+ 一天一万多行,文件里只收它 WARNING 以上的。
"""
try:
path.parent.mkdir(parents=True, exist_ok=True)
@@ -1400,6 +1401,8 @@ def log_to_file(path: Path) -> logging.Handler | None:
except OSError:
return None
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
+ handler.addFilter(lambda record: record.levelno >= logging.WARNING
+ or not record.name.startswith(("httpx", "httpcore")))
logging.getLogger().addHandler(handler)
return handler
diff --git a/tests/test_tray.py b/tests/test_tray.py
index 1d8b21efe..df32d5635 100644
--- a/tests/test_tray.py
+++ b/tests/test_tray.py
@@ -2,6 +2,7 @@ import contextlib
import importlib.util
import inspect
import io
+import logging
import os
import plistlib
import re
@@ -1315,5 +1316,29 @@ class ServiceRevivalTests(unittest.TestCase):
self.assertTrue(all(handle.closed for handle in handed))
+class TrayLogFileTests(unittest.TestCase):
+ def test_the_file_keeps_tray_events_and_drops_per_probe_http_noise(self):
+ """健康检查十秒一轮,httpx 的 INFO 进文件的话一天一万多行,真正的启停记录被淹掉。"""
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / "logs" / "tray.log"
+ root = logging.getLogger()
+ level = root.level
+ root.setLevel(logging.INFO)
+ handler = tray_module.log_to_file(path)
+ self.assertIsNotNone(handler)
+ try:
+ logging.getLogger("httpx").info("HTTP Request: GET http://127.0.0.1/healthz")
+ logging.getLogger("httpx").warning("连接被拒绝")
+ logging.getLogger("peach.tray").info("拉起 http 服务,PID 1")
+ finally:
+ root.removeHandler(handler)
+ handler.close()
+ root.setLevel(level)
+ text = path.read_text(encoding="utf-8")
+ self.assertNotIn("HTTP Request", text)
+ self.assertIn("连接被拒绝", text)
+ self.assertIn("拉起 http 服务", text)
+
+
if __name__ == "__main__":
unittest.main()