PaiGram/plugins/system/log.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
2.2 KiB
Python
Raw Normal View History

2022-09-18 08:11:09 +00:00
import os
from telegram import Update
from telegram.constants import ChatAction
from telegram.ext import CallbackContext
2022-09-18 08:11:09 +00:00
2022-11-21 14:45:27 +00:00
from core.config import config
2022-09-18 08:11:09 +00:00
from core.plugin import Plugin, handler
2022-11-21 14:45:27 +00:00
from modules.errorpush import PbClient, PbClientException
2022-09-18 08:16:33 +00:00
from utils.log import logger
2022-09-18 08:11:09 +00:00
current_dir = os.getcwd()
error_log = os.path.join(current_dir, "logs", "error", "error.log")
debug_log = os.path.join(current_dir, "logs", "debug", "debug.log")
class Log(Plugin):
def __init__(self):
2022-11-21 14:45:27 +00:00
self.pb_client = PbClient(config.error.pb_url, 3600, 10000)
async def send_to_pb(self, file_name: str):
pb_url = ""
try:
with open(file_name, "r", encoding="utf-8") as f:
pb_url = await self.pb_client.create_pb(f.read())
2022-11-21 14:45:27 +00:00
except PbClientException as exc:
logger.warning("上传错误信息至 fars 失败", exc_info=exc)
except Exception as exc:
logger.error("上传错误信息至 fars 失败")
logger.exception(exc)
return pb_url
@handler.command(command="send_log", block=False, admin=True)
2022-09-18 08:11:09 +00:00
async def send_log(self, update: Update, _: CallbackContext):
2022-09-18 08:16:33 +00:00
user = update.effective_user
logger.info("用户 %s[%s] send_log 命令请求", user.full_name, user.id)
2022-09-18 08:11:09 +00:00
message = update.effective_message
2022-10-06 16:41:38 +00:00
if os.path.exists(error_log) and os.path.getsize(error_log) > 0:
pb_url = await self.send_to_pb(error_log)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(
open(error_log, mode="rb+"), caption=f"Error Log\n{pb_url}/text" if pb_url else "Error Log"
)
2022-09-18 08:11:09 +00:00
else:
await message.reply_text("错误日记未找到")
2022-10-06 16:41:38 +00:00
if os.path.exists(debug_log) and os.path.getsize(debug_log) > 0:
pb_url = await self.send_to_pb(debug_log)
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT)
await message.reply_document(
open(debug_log, mode="rb+"), caption=f"Debug Log\n{pb_url}/text" if pb_url else "Debug Log"
)
2022-09-18 08:11:09 +00:00
else:
await message.reply_text("调试日记未找到")