mirror of
https://github.com/dnslin/aria2bot.git
synced 2026-01-12 04:22:21 +08:00
feat(logging): 为 aria2 安装器、服务和 Telegram 处理器添加详细日志记录
This commit is contained in:
@@ -4,6 +4,8 @@ from __future__ import annotations
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes, CommandHandler
|
||||
|
||||
from src.utils.logger import get_logger
|
||||
|
||||
from src.core import (
|
||||
Aria2Config,
|
||||
Aria2Error,
|
||||
@@ -17,6 +19,16 @@ from src.core import (
|
||||
)
|
||||
from src.aria2 import Aria2Installer, Aria2ServiceManager
|
||||
|
||||
logger = get_logger("handlers")
|
||||
|
||||
|
||||
def _get_user_info(update: Update) -> str:
|
||||
"""获取用户信息用于日志"""
|
||||
user = update.effective_user
|
||||
if user:
|
||||
return f"用户ID={user.id}, 用户名={user.username or 'N/A'}"
|
||||
return "未知用户"
|
||||
|
||||
|
||||
class Aria2BotAPI:
|
||||
def __init__(self, config: Aria2Config | None = None):
|
||||
@@ -61,6 +73,7 @@ class Aria2BotAPI:
|
||||
return self.config.rpc_port
|
||||
|
||||
async def install(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /install 命令 - {_get_user_info(update)}")
|
||||
await self._reply(update, context, "正在安装 aria2,处理中,请稍候...")
|
||||
try:
|
||||
result = await self.installer.install()
|
||||
@@ -82,12 +95,16 @@ class Aria2BotAPI:
|
||||
]
|
||||
),
|
||||
)
|
||||
logger.info(f"/install 命令执行成功 - {_get_user_info(update)}")
|
||||
except (DownloadError, ConfigError, Aria2Error) as exc:
|
||||
logger.error(f"/install 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"安装失败:{exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/install 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"安装失败,发生未知错误:{exc}")
|
||||
|
||||
async def uninstall(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /uninstall 命令 - {_get_user_info(update)}")
|
||||
await self._reply(update, context, "正在卸载 aria2,处理中,请稍候...")
|
||||
try:
|
||||
try:
|
||||
@@ -96,53 +113,73 @@ class Aria2BotAPI:
|
||||
pass
|
||||
self.installer.uninstall()
|
||||
await self._reply(update, context, "卸载完成 ✅")
|
||||
logger.info(f"/uninstall 命令执行成功 - {_get_user_info(update)}")
|
||||
except Aria2Error as exc:
|
||||
logger.error(f"/uninstall 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"卸载失败:{exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/uninstall 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"卸载失败,发生未知错误:{exc}")
|
||||
|
||||
async def start_service(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /start 命令 - {_get_user_info(update)}")
|
||||
try:
|
||||
if not is_aria2_installed():
|
||||
logger.info(f"/start 命令: aria2 未安装 - {_get_user_info(update)}")
|
||||
await self._reply(update, context, "aria2 未安装,请先运行 /install")
|
||||
return
|
||||
self.service.start()
|
||||
await self._reply(update, context, "aria2 服务已启动 ✅")
|
||||
logger.info(f"/start 命令执行成功 - {_get_user_info(update)}")
|
||||
except NotInstalledError:
|
||||
logger.info(f"/start 命令: aria2 未安装 - {_get_user_info(update)}")
|
||||
await self._reply(update, context, "aria2 未安装,请先运行 /install")
|
||||
except ServiceError as exc:
|
||||
logger.error(f"/start 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"启动失败:{exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/start 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"启动失败,发生未知错误:{exc}")
|
||||
|
||||
async def stop_service(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /stop 命令 - {_get_user_info(update)}")
|
||||
try:
|
||||
self.service.stop()
|
||||
await self._reply(update, context, "aria2 服务已停止 ✅")
|
||||
logger.info(f"/stop 命令执行成功 - {_get_user_info(update)}")
|
||||
except ServiceError as exc:
|
||||
logger.error(f"/stop 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"停止失败:{exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/stop 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"停止失败,发生未知错误:{exc}")
|
||||
|
||||
async def restart_service(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /restart 命令 - {_get_user_info(update)}")
|
||||
try:
|
||||
self.service.restart()
|
||||
await self._reply(update, context, "aria2 服务已重启 ✅")
|
||||
logger.info(f"/restart 命令执行成功 - {_get_user_info(update)}")
|
||||
except ServiceError as exc:
|
||||
logger.error(f"/restart 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"重启失败:{exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/restart 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"重启失败,发生未知错误:{exc}")
|
||||
|
||||
async def status(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /status 命令 - {_get_user_info(update)}")
|
||||
try:
|
||||
info = self.service.status()
|
||||
version = get_aria2_version() or "未知"
|
||||
rpc_secret = self._get_rpc_secret() or "未设置"
|
||||
rpc_port = self._get_rpc_port() or self.config.rpc_port or "未知"
|
||||
except ServiceError as exc:
|
||||
logger.error(f"/status 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"获取状态失败:{exc}")
|
||||
return
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/status 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"获取状态失败,发生未知错误:{exc}")
|
||||
return
|
||||
|
||||
@@ -156,33 +193,44 @@ class Aria2BotAPI:
|
||||
f"- RPC 密钥:`{rpc_secret}`"
|
||||
)
|
||||
await self._reply(update, context, text, parse_mode="Markdown")
|
||||
logger.info(f"/status 命令执行成功 - {_get_user_info(update)}")
|
||||
|
||||
async def view_logs(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /logs 命令 - {_get_user_info(update)}")
|
||||
try:
|
||||
logs = self.service.view_log(lines=30)
|
||||
except ServiceError as exc:
|
||||
logger.error(f"/logs 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"读取日志失败:{exc}")
|
||||
return
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/logs 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"读取日志失败,发生未知错误:{exc}")
|
||||
return
|
||||
|
||||
if not logs.strip():
|
||||
await self._reply(update, context, "暂无日志内容。")
|
||||
logger.info(f"/logs 命令执行成功(无日志) - {_get_user_info(update)}")
|
||||
return
|
||||
|
||||
await self._reply(update, context, f"最近 30 行日志:\n{logs}")
|
||||
logger.info(f"/logs 命令执行成功 - {_get_user_info(update)}")
|
||||
|
||||
async def clear_logs(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /clear_logs 命令 - {_get_user_info(update)}")
|
||||
try:
|
||||
self.service.clear_log()
|
||||
await self._reply(update, context, "日志已清空 ✅")
|
||||
logger.info(f"/clear_logs 命令执行成功 - {_get_user_info(update)}")
|
||||
except ServiceError as exc:
|
||||
logger.error(f"/clear_logs 命令执行失败: {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"清空日志失败:{exc}")
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(f"/clear_logs 命令执行失败(未知错误): {exc} - {_get_user_info(update)}")
|
||||
await self._reply(update, context, f"清空日志失败,发生未知错误:{exc}")
|
||||
|
||||
async def help_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.info(f"收到 /help 命令 - {_get_user_info(update)}")
|
||||
commands = [
|
||||
"/install - 安装 aria2",
|
||||
"/uninstall - 卸载 aria2",
|
||||
|
||||
Reference in New Issue
Block a user