From dd69a68b0f7c0f21e59a852fd8df0c9581ba0fe8 Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Fri, 20 Feb 2026 23:40:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(code-review):=20=E5=8F=82=E8=80=83=E5=BB=BA?= =?UTF-8?q?=E8=AE=AE=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 22 ---------------------- src/utils/logger.py | 41 +++++++++++++++++++++++++---------------- src/utils/pdf_utils.py | 6 ++---- 3 files changed, 27 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index a4cb3a9..e3edc59 100644 --- a/main.py +++ b/main.py @@ -752,25 +752,3 @@ class QQGroupDailyAnalysis(Star): f"• 参与者: {summary['participants']}\n" f"• 高峰时段: {summary['peak_hours']}" ) - - def _get_group_id_from_event(self, event: AstrMessageEvent) -> str | None: - """从消息事件中安全获取群组 ID""" - try: - group_id = event.get_group_id() - return group_id if group_id else None - except Exception: - return None - - def _get_platform_id_from_event(self, event: AstrMessageEvent) -> str: - """从消息事件中获取平台唯一 ID""" - try: - return event.get_platform_id() - except Exception: - # 后备方案:从元数据获取 - if ( - hasattr(event, "platform_meta") - and event.platform_meta - and hasattr(event.platform_meta, "id") - ): - return event.platform_meta.id - return "default" diff --git a/src/utils/logger.py b/src/utils/logger.py index 18530ad..c31d761 100644 --- a/src/utils/logger.py +++ b/src/utils/logger.py @@ -1,30 +1,39 @@ -import logging -from typing import Any - from astrbot.api import logger as astrbot_logger -class PluginLoggerAdapter(logging.LoggerAdapter): +class PluginLogger: """ - 日志适配器:插件级统一日志装饰器 + 日志代理类:插件级统一日志装饰器 自动向所有通过该实例输出的日志信息前缀添加 `[QQ群分析]` 标签, 以便用户在 AstrBot 混合日志流中快速定位属于本插件的输出。 + 不直接继承 logging.LoggerAdapter 以符合框架规范。 """ - def process(self, msg: str, kwargs: Any) -> tuple[str, Any]: - """ - 加工日志消息,注入插件专有前缀。 + def __init__(self, prefix: str = "[QQ群分析]"): + self.prefix = prefix - Args: - msg (str): 原始日志消息 - kwargs (Any): 额外的日志参数映射 + def _format_msg(self, msg: str) -> str: + return f"{self.prefix} {msg}" - Returns: - tuple[str, Any]: (格式化后的消息, 参数) - """ - return f"[QQ群分析] {msg}", kwargs + def info(self, msg: str, *args, **kwargs): + astrbot_logger.info(self._format_msg(msg), *args, **kwargs) + + def error(self, msg: str, *args, **kwargs): + astrbot_logger.error(self._format_msg(msg), *args, **kwargs) + + def warning(self, msg: str, *args, **kwargs): + astrbot_logger.warning(self._format_msg(msg), *args, **kwargs) + + def debug(self, msg: str, *args, **kwargs): + astrbot_logger.debug(self._format_msg(msg), *args, **kwargs) + + def critical(self, msg: str, *args, **kwargs): + astrbot_logger.critical(self._format_msg(msg), *args, **kwargs) + + def exception(self, msg: str, *args, **kwargs): + astrbot_logger.exception(self._format_msg(msg), *args, **kwargs) # 导出带前缀的 logger -logger = PluginLoggerAdapter(astrbot_logger, {}) +logger = PluginLogger() diff --git a/src/utils/pdf_utils.py b/src/utils/pdf_utils.py index fa85a8b..1f211c3 100644 --- a/src/utils/pdf_utils.py +++ b/src/utils/pdf_utils.py @@ -6,6 +6,7 @@ PDF工具模块 import asyncio import sys from concurrent.futures import ThreadPoolExecutor +from pathlib import Path from typing import Any from .logger import logger @@ -72,9 +73,6 @@ class PDFInstaller: logger.info("第一步完成。正在检查浏览器内核...") - # 2. 检查自定义路径:若用户已手动提供内核,则跳过下载步骤 - from pathlib import Path - custom_path = config_manager.get_browser_path() if custom_path and Path(custom_path).exists(): logger.info(f"检测到自定义浏览器路径: {custom_path}。跳过内核下载。") @@ -127,7 +125,7 @@ class PDFInstaller: @staticmethod async def _background_playwright_install() -> None: """ - 底层宿主任务:驱动系统 shell 执行浏览器二进制文件部署。 + 底层宿主任务:驱动 system shell 执行浏览器二进制文件部署。 """ try: logger.info("正在执行二进制文件:playwright install chromium")