From 851c9eb3c7b0f5371794e7cda9547f2d8cd59a96 Mon Sep 17 00:00:00 2001 From: passthem Date: Fri, 24 Oct 2025 00:01:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A8=8B=E5=BA=8F=E9=80=80?= =?UTF-8?q?=E5=87=BA=E8=80=97=E6=97=B6=E5=A4=AA=E4=B9=85=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 6 +++++- konabot/common/log.py | 11 ++++++----- konabot/plugins/simple_notify/__init__.py | 22 +++------------------- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/bot.py b/bot.py index 287bf2d..782c870 100644 --- a/bot.py +++ b/bot.py @@ -20,9 +20,13 @@ env_enable_minecraft = os.environ.get("ENABLE_MINECRAFT", "none") def main(): + if env.upper() == 'DEBUG' or env.upper() == 'DEV': + console_log_level = 'DEBUG' + else: + console_log_level = 'INFO' init_logger(LOG_PATH, [ BotExceptionMessage, - ]) + ], console_log_level=console_log_level) nonebot.init() diff --git a/konabot/common/log.py b/konabot/common/log.py index 753dbac..96e3305 100644 --- a/konabot/common/log.py +++ b/konabot/common/log.py @@ -18,7 +18,7 @@ def file_exception_filter( 否则,返回 True(允许记录)。 """ exception_info = record.get("exception") - + if exception_info: exception_type = exception_info[0] @@ -29,8 +29,9 @@ def file_exception_filter( def init_logger( - log_dir: Path, - ignored_exceptions: List[Type[Exception]] + log_dir: Path, + ignored_exceptions: List[Type[Exception]], + console_log_level: str = "INFO", ) -> None: """ 配置全局 Loguru Logger。 @@ -47,7 +48,7 @@ def init_logger( logger.add( sys.stderr, - level="INFO", + level=console_log_level, colorize=True, format="{time:HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}", ) @@ -76,4 +77,4 @@ def init_logger( ) logger.info("Loguru Logger 初始化完成!") - logger.info(f"控制台日志级别: INFO") + logger.info(f"控制台日志级别: {console_log_level}") diff --git a/konabot/plugins/simple_notify/__init__.py b/konabot/plugins/simple_notify/__init__.py index 7ceeb86..6e85894 100644 --- a/konabot/plugins/simple_notify/__init__.py +++ b/konabot/plugins/simple_notify/__init__.py @@ -1,10 +1,8 @@ import asyncio as asynkio import datetime -import functools from pathlib import Path from typing import Any, Literal, cast -import signal import nonebot import ptimeparse from loguru import logger @@ -123,7 +121,9 @@ def create_notify_task(notify: Notify, fail2remove: bool = True): try: await asynkio.sleep((notify.notify_time - begin_time).total_seconds()) except asynkio.CancelledError: - logger.debug("代办提醒被信号中止,任务退出") + logger.debug( + f"代办提醒被信号中止,任务退出 NOTIFY={notify.notify_msg} TIME={notify.notify_time}" + ) return else: logger.warning( @@ -254,19 +254,3 @@ async def _(): save_notify_config(cfg) DATA_FILE_LOCK.release() - loop = asynkio.get_running_loop() - - # 解决 asynk task 没有被 cancel 的问题 - async def shutdown(sig: signal.Signals): - logger.info(f"收到 {sig.name} 指令,正在关闭所有的东西") - for task in ASYNK_TASKS: - task.cancel() - await asynkio.gather(*ASYNK_TASKS, return_exceptions=True) - logger.info("所有的代办提醒 Task 都已经退出了") - - for sig in (signal.SIGINT, signal.SIGTERM): - loop.add_signal_handler( - sig, functools.partial(asynkio.create_task, shutdown(sig)) - ) - - await asynkio.gather(*ASYNK_TASKS)