From b0a77ad286a684f5cc7b7b0ada7f2f68c9779c69 Mon Sep 17 00:00:00 2001 From: ngfchl Date: Mon, 6 Jul 2026 20:52:21 +0800 Subject: [PATCH] fix: reduce polling and access log noise --- main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 8ecd05f..c3ab8f0 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 """TG 群管机器人 v2 — FastAPI + Tortoise ORM + python-telegram-bot""" import os +import time import asyncio import logging from contextlib import asynccontextmanager @@ -35,6 +36,18 @@ logging.getLogger("httpcore").setLevel(logging.WARNING) # ============ Telegram Bot ============ tg_app: Application = None +_last_polling_error_log = 0.0 + + +def polling_error_callback(exc): + """压缩 Telegram polling 网络异常日志,避免代理偶发断连刷 traceback。""" + global _last_polling_error_log + now = time.time() + # polling 会自动重试;同类网络波动 60 秒内只提示一次。 + if now - _last_polling_error_log < 60: + return + _last_polling_error_log = now + logger.warning(f"⚠️ TG polling 网络波动,已自动重试: {type(exc).__name__}: {exc}") async def start_tg_bot(): @@ -54,7 +67,7 @@ async def start_tg_bot(): await tg_app.initialize() await tg_app.start() - await tg_app.updater.start_polling(drop_pending_updates=False) + await tg_app.updater.start_polling(drop_pending_updates=False, error_callback=polling_error_callback) # 菜单 commands = [ @@ -121,7 +134,7 @@ web_app.router.lifespan_context = lifespan def run(): - uvicorn.run(web_app, host="0.0.0.0", port=config.WEB_PORT, log_level="info") + uvicorn.run(web_app, host="0.0.0.0", port=config.WEB_PORT, log_level="info", access_log=False) if __name__ == "__main__":