feat(logger, telegram_bot): Add real-time Telegram logging capability

- Add TelegramLogHandler class to stream logs to Telegram in real-time
- Implement enable_telegram_logging() method to activate Telegram log streaming
- Implement disable_telegram_logging() method to deactivate Telegram log streaming
- Implement is_telegram_logging_enabled() method to check logging status
- Add /logs_live command to enable real-time log push notifications
- Add /logs_stop command to disable real-time log push notifications
- Update help text to document new real-time logging commands
- Remove obsolete team.json.example file
- Enables administrators to monitor system logs in real-time through Telegram bot
This commit is contained in:
2026-01-18 01:16:20 +08:00
parent b902922d22
commit ad120687b3
3 changed files with 92 additions and 40 deletions

View File

@@ -102,6 +102,8 @@ class ProvisionerBot:
("run_all", self.cmd_run_all),
("stop", self.cmd_stop),
("logs", self.cmd_logs),
("logs_live", self.cmd_logs_live),
("logs_stop", self.cmd_logs_stop),
("dashboard", self.cmd_dashboard),
("import", self.cmd_import),
("stock", self.cmd_stock),
@@ -204,6 +206,8 @@ class ProvisionerBot:
/team <n> - 查看第 n 个 Team 处理详情
/config - 查看系统配置
/logs [n] - 查看最近 n 条日志
/logs_live - 启用实时日志推送
/logs_stop - 停止实时日志推送
<b>🚀 任务控制:</b>
/run &lt;n&gt; - 开始处理第 n 个 Team
@@ -849,6 +853,40 @@ class ProvisionerBot:
except Exception as e:
await update.message.reply_text(f"❌ 读取日志失败: {e}")
@admin_only
async def cmd_logs_live(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""启用实时日志推送"""
from logger import log
if log.is_telegram_logging_enabled():
await update.message.reply_text("📡 实时日志已经在运行中")
return
# 启用 Telegram 日志推送
def log_callback(message: str, level: str):
"""日志回调函数"""
if self.notifier:
self.notifier.queue_message(message, level)
log.enable_telegram_logging(log_callback)
await update.message.reply_text(
"✅ 实时日志已启用\n"
"所有日志将实时推送到此聊天\n"
"使用 /logs_stop 停止推送"
)
@admin_only
async def cmd_logs_stop(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""停止实时日志推送"""
from logger import log
if not log.is_telegram_logging_enabled():
await update.message.reply_text("📭 实时日志未启用")
return
log.disable_telegram_logging()
await update.message.reply_text("✅ 实时日志已停止")
@admin_only
async def cmd_dashboard(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""查看 S2A 仪表盘统计"""