fix: reduce polling and access log noise

This commit is contained in:
ngfchl
2026-07-06 20:52:21 +08:00
parent a73eb9a1e1
commit b0a77ad286
+15 -2
View File
@@ -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__":