From 13b95c273257dede37a7c19c211e2399ab8dce55 Mon Sep 17 00:00:00 2001 From: snowykami Date: Sun, 20 Oct 2024 20:59:30 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E7=BB=86=E8=8A=82=E5=B0=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/bot/iobus.py | 0 liteyuki/comm/channel.py | 5 - liteyuki/config.py | 2 + liteyuki/log.py | 57 ++++--- liteyuki/session/__init__.py | 5 +- requirements.txt | 6 +- src/liteyuki_plugins/nonebot/__init__.py | 6 +- .../nonebot_plugin_gotify/__init__.py | 2 +- .../nonebot/nonebot_plugins/to_liteyuki.py | 9 +- src/liteyuki_plugins/nonebot/np_main/core.py | 9 +- .../nonebot/np_main/loader.py | 2 - src/utils/__init__.py | 2 - src/utils/base/log.py | 158 +++++++++--------- tests/test_logger.py | 9 + 14 files changed, 148 insertions(+), 124 deletions(-) delete mode 100644 liteyuki/bot/iobus.py create mode 100644 tests/test_logger.py diff --git a/liteyuki/bot/iobus.py b/liteyuki/bot/iobus.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/liteyuki/comm/channel.py b/liteyuki/comm/channel.py index 2ca9449c0..a1105d0dc 100644 --- a/liteyuki/comm/channel.py +++ b/liteyuki/comm/channel.py @@ -75,11 +75,6 @@ def __init__(self, name: str, type_check: Optional[bool] = None): if name in _channel: raise ValueError(f"Channel {name} already exists") _channel[name] = self - logger.debug(f"Channel {name} initialized in main process") - else: - logger.debug( - f"Channel {name} initialized in sub process, should manually set in main process" - ) def _get_generic_type(self) -> Optional[type]: """ diff --git a/liteyuki/config.py b/liteyuki/config.py index b55298f62..39c09dea3 100644 --- a/liteyuki/config.py +++ b/liteyuki/config.py @@ -118,6 +118,8 @@ def load_config_in_default(no_waring: bool = False) -> dict[str, Any]: 从一个标准的轻雪项目加载配置文件 项目目录下的config.*和config目录下的所有配置文件 项目目录下的配置文件优先 + Args: + no_waring: 是否关闭警告 """ config = load_configs_from_dirs("config", no_waring=no_waring) config.update( diff --git a/liteyuki/log.py b/liteyuki/log.py index e6ca52308..b0d2d776f 100644 --- a/liteyuki/log.py +++ b/liteyuki/log.py @@ -12,26 +12,34 @@ import loguru -logger = loguru.logger +logger = loguru.logger.bind() -# DEBUG日志格式 debug_format: str = ( - "{time:YYYY-MM-DD HH:mm:ss} " - "[{level.icon}] " - "<{name}.{module}.{function}:{line}> " - "{message}" + "{time:YYYY-MM-DD HH:mm:ss} " + "[{level.icon}{level}] " + "<{name}.{module}.{function}:{line}> " + "{message}" ) # 默认日志格式 default_format: str = ( - "{time:MM-DD HH:mm:ss} " - "[{level.icon}] " - "<{name}> " - "{message}" + "{time:MM-DD HH:mm:ss} " + "[{level.icon}{level}] " + "<{name}> " + "{message}" ) - def get_format(level: str) -> str: + """ + 获取日志格式 + Args: + level: 日志等级 + + Returns: 日志格式 + + """ + # DEBUG日志格式 + if level == "DEBUG": return debug_format else: @@ -41,23 +49,28 @@ def get_format(level: str) -> str: def init_log(config: dict): """ 在语言加载完成后执行 - Returns: - + Args: + config: 配置 """ - + global logger + level = config.get("log_level", "DEBUG") + print("初始化日志系统", level) logger.remove() logger.add( sys.stdout, - level=0, + level=level, diagnose=False, - format=get_format(config.get("log_level", "INFO")), + format=get_format(level), ) show_icon = config.get("log_icon", True) - logger.level("DEBUG", color="", icon=f"{'🐛' if show_icon else ''}DEBUG") - logger.level("INFO", color="", icon=f"{'ℹ️' if show_icon else ''}INFO") - logger.level("SUCCESS", color="", icon=f"{'✅' if show_icon else ''}SUCCESS") - logger.level("WARNING", color="", icon=f"{'⚠️' if show_icon else ''}WARNING") - logger.level("ERROR", color="", icon=f"{'⭕' if show_icon else ''}ERROR") + logger.level("DEBUG", color="", icon=f"{'🐛' if show_icon else ''}") + logger.level("INFO", color="", icon=f"{'ℹ️' if show_icon else ''}") + logger.level("SUCCESS", color="", icon=f"{'✅' if show_icon else ''}") + logger.level("WARNING", color="", icon=f"{'⚠️' if show_icon else ''}") + logger.level("ERROR", color="", icon=f"{'⭕' if show_icon else ''}") + logger.level("CRITICAL", color="", icon=f"{'❌' if show_icon else ''}") + logger.level("TRACE", color="", icon=f"{'🔍' if show_icon else ''}") + logger.bind() -init_log(config={}) +init_log(config={"log_level": "DEBUG", "log_icon": True}) \ No newline at end of file diff --git a/liteyuki/session/__init__.py b/liteyuki/session/__init__.py index 64879d5c4..75568f42c 100644 --- a/liteyuki/session/__init__.py +++ b/liteyuki/session/__init__.py @@ -7,6 +7,8 @@ from magicoca import Chan, select +from liteyuki.log import logger + def message_handler_thread(i_chans: Iterable[Chan[Any]]): """ @@ -15,4 +17,5 @@ def message_handler_thread(i_chans: Iterable[Chan[Any]]): Returns: """ for msg in select(*i_chans): - print("Recv from anybot", msg) \ No newline at end of file + logger.debug(f"Recv from anybot {msg}") + logger.info(f"Recv from anybot {msg}") \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 67a918342..5dcfd31ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,9 +7,10 @@ httpx>=0.27.0 nonebot-plugin-htmlrender>=0.1.0 nonebot2[fastapi,httpx,websockets]>=2.3.3 nonebot-adapter-onebot>=2.4.3 -nonebot-plugin-alconna>=0.46.3 +nonebot-plugin-alconna>=0.53.0 nonebot_plugin_apscheduler>=0.4.0 nonebot-adapter-satori>=0.11.5 +mysql-connector-python>=9.1.0 pyppeteer>=2.0.0 markdown>=3.3.6 zhDateTime>=1.0.3 @@ -34,4 +35,5 @@ fastapi~=0.115.0 # liteyuki dependencies croterline>=1.0.7 -magicoca>=1.0.5 \ No newline at end of file +magicoca>=1.0.5 +mysql-connector-python~=9.1.0 \ No newline at end of file diff --git a/src/liteyuki_plugins/nonebot/__init__.py b/src/liteyuki_plugins/nonebot/__init__.py index fcab9bd8d..68bcad032 100644 --- a/src/liteyuki_plugins/nonebot/__init__.py +++ b/src/liteyuki_plugins/nonebot/__init__.py @@ -1,7 +1,7 @@ import os.path from pathlib import Path -import nonebot + from croterline.utils import IsMainProcess from liteyuki.core import sub_process_manager @@ -14,9 +14,12 @@ def nb_run(*args, **kwargs): + import nonebot + nonebot.init(**kwargs) from .nb_utils import driver_manager, adapter_manager + driver_manager.init(config=kwargs) adapter_manager.init(kwargs) adapter_manager.register() @@ -26,6 +29,7 @@ def nb_run(*args, **kwargs): if IsMainProcess: from .dev_reloader import * + bot = get_bot() sub_process_manager.add( diff --git a/src/liteyuki_plugins/nonebot/nonebot_plugins/nonebot_plugin_gotify/__init__.py b/src/liteyuki_plugins/nonebot/nonebot_plugins/nonebot_plugin_gotify/__init__.py index fdc6b9563..46d5f0e3d 100644 --- a/src/liteyuki_plugins/nonebot/nonebot_plugins/nonebot_plugin_gotify/__init__.py +++ b/src/liteyuki_plugins/nonebot/nonebot_plugins/nonebot_plugin_gotify/__init__.py @@ -50,7 +50,7 @@ async def start_push_thread(): ) if MESSAGE in plugin_config.gotify_includes: - @on_message().handle() + @on_message(block=False, priority=100).handle() async def _(event: Event): ctx = Context( user_id=event.get_user_id(), diff --git a/src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py b/src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py index fc31a169f..cf6e34edb 100644 --- a/src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py +++ b/src/liteyuki_plugins/nonebot/nonebot_plugins/to_liteyuki.py @@ -12,6 +12,7 @@ from croterline.process import get_ctx from nonebot.adapters.onebot.v11 import MessageEvent from nonebot.plugin import PluginMetadata +from nonebot.log import logger from nonebot import on_message __plugin_meta__ = PluginMetadata( @@ -20,11 +21,13 @@ usage="用户无需使用", ) + + ctx = get_ctx() -@on_message().handle() +@on_message(block=False, priority=100).handle() async def _(event: MessageEvent): - print("Push message to Liteyuki") - + logger.debug("Pushing message to Liteyuki") ctx.sub_chan << event.raw_message + logger.debug("Pushed message to Liteyuki") diff --git a/src/liteyuki_plugins/nonebot/np_main/core.py b/src/liteyuki_plugins/nonebot/np_main/core.py index a576e3398..08330426f 100644 --- a/src/liteyuki_plugins/nonebot/np_main/core.py +++ b/src/liteyuki_plugins/nonebot/np_main/core.py @@ -1,9 +1,6 @@ import time from typing import AnyStr -import time -from typing import AnyStr - import nonebot import pip from nonebot import get_driver, require @@ -14,15 +11,15 @@ # from src.liteyuki.core import Reloader from src.utils import event as event_utils, satori_utils +from src.utils.base import reload # type: ignore from src.utils.base.config import get_config from src.utils.base.data_manager import TempConfig, common_db from src.utils.base.language import get_user_lang +from src.utils.base.ly_function import get_function # type: ignore from src.utils.base.ly_typing import T_Bot, T_MessageEvent +from src.utils.message.html_tool import md_to_pic from src.utils.message.message import MarkdownMessage as md, broadcast_to_superusers from .api import update_liteyuki # type: ignore -from src.utils.base import reload # type: ignore -from src.utils.base.ly_function import get_function # type: ignore -from src.utils.message.html_tool import md_to_pic require("nonebot_plugin_alconna") require("nonebot_plugin_apscheduler") diff --git a/src/liteyuki_plugins/nonebot/np_main/loader.py b/src/liteyuki_plugins/nonebot/np_main/loader.py index 498f89b0a..d77c4dd31 100644 --- a/src/liteyuki_plugins/nonebot/np_main/loader.py +++ b/src/liteyuki_plugins/nonebot/np_main/loader.py @@ -4,14 +4,12 @@ import nonebot.plugin from nonebot import get_driver -from src.utils import init_log from src.utils.base.config import get_config from src.utils.base.data_manager import InstalledPlugin, plugin_db from src.utils.base.resource import load_resources from src.utils.message.tools import check_for_package load_resources() -init_log() driver = get_driver() diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 5f0237f5e..d04ed75e1 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -6,7 +6,6 @@ __VERSION__ = "6.3.2" # 60201 from src.utils.base.config import load_from_yaml, config -from src.utils.base.log import init_log from git import Repo major, minor, patch = map(int, __VERSION__.split(".")) @@ -20,7 +19,6 @@ def init(): """ # 检测python版本是否高于3.10 - init_log() if sys.version_info < (3, 10): nonebot.logger.error("Requires Python3.10+ to run, please upgrade your Python Environment.") exit(1) diff --git a/src/utils/base/log.py b/src/utils/base/log.py index 3e2b09ab4..2364122d2 100644 --- a/src/utils/base/log.py +++ b/src/utils/base/log.py @@ -1,79 +1,79 @@ -import sys -import loguru -from typing import TYPE_CHECKING -from .config import load_from_yaml -from .language import Language, get_default_lang_code - -logger = loguru.logger -if TYPE_CHECKING: - # avoid sphinx autodoc resolve annotation failed - # because loguru module do not have `Logger` class actually - from loguru import Record - - -def default_filter(record: "Record"): - """默认的日志过滤器,根据 `config.log_level` 配置改变日志等级。""" - log_level = record["extra"].get("nonebot_log_level", "INFO") - levelno = logger.level(log_level).no if isinstance(log_level, str) else log_level - return record["level"].no >= levelno - - -# DEBUG日志格式 -debug_format: str = ( - "{time:YYYY-MM-DD HH:mm:ss} " - "[{level.icon}] " - "<{name}.{module}.{function}:{line}> " - "{message}" -) - -# 默认日志格式 -default_format: str = ( - "{time:MM-DD HH:mm:ss} " - "[{level.icon}] " - "<{name}> " - "{message}" -) - - -def get_format(level: str) -> str: - if level == "DEBUG": - return debug_format - else: - return default_format - - -logger = loguru.logger.bind() - - -def init_log(): - """ - 在语言加载完成后执行 - Returns: - - """ - global logger - - config = load_from_yaml("config.yml") - - logger.remove() - logger.add( - sys.stdout, - level=0, - diagnose=False, - filter=default_filter, - format=get_format(config.get("log_level", "INFO")), - ) - show_icon = config.get("log_icon", True) - lang = Language(get_default_lang_code()) - - debug = lang.get("log.debug", default="==DEBUG") - info = lang.get("log.info", default="===INFO") - success = lang.get("log.success", default="SUCCESS") - warning = lang.get("log.warning", default="WARNING") - error = lang.get("log.error", default="==ERROR") - - logger.level("DEBUG", color="", icon=f"{'🐛' if show_icon else ''}{debug}") - logger.level("INFO", color="", icon=f"{'ℹ️' if show_icon else ''}{info}") - logger.level("SUCCESS", color="", icon=f"{'✅' if show_icon else ''}{success}") - logger.level("WARNING", color="", icon=f"{'⚠️' if show_icon else ''}{warning}") - logger.level("ERROR", color="", icon=f"{'⭕' if show_icon else ''}{error}") +# import sys +# import loguru +# from typing import TYPE_CHECKING +# from .config import load_from_yaml +# from .language import Language, get_default_lang_code +# +# logger = loguru.logger +# if TYPE_CHECKING: +# # avoid sphinx autodoc resolve annotation failed +# # because loguru module do not have `Logger` class actually +# from loguru import Record +# +# +# def default_filter(record: "Record"): +# """默认的日志过滤器,根据 `config.log_level` 配置改变日志等级。""" +# log_level = record["extra"].get("nonebot_log_level", "INFO") +# levelno = logger.level(log_level).no if isinstance(log_level, str) else log_level +# return record["level"].no >= levelno +# +# +# # DEBUG日志格式 +# debug_format: str = ( +# "{time:YYYY-MM-DD HH:mm:ss} " +# "[{level.icon}] " +# "<{name}.{module}.{function}:{line}> " +# "{message}" +# ) +# +# # 默认日志格式 +# default_format: str = ( +# "{time:MM-DD HH:mm:ss} " +# "[{level.icon}] " +# "<{name}> " +# "{message}" +# ) +# +# +# def get_format(level: str) -> str: +# if level == "DEBUG": +# return debug_format +# else: +# return default_format +# +# +# logger = loguru.logger.bind() +# +# +# def init_log(): +# """ +# 在语言加载完成后执行 +# Returns: +# +# """ +# global logger +# +# config = load_from_yaml("config.yml") +# +# logger.remove() +# logger.add( +# sys.stdout, +# level=0, +# diagnose=False, +# filter=default_filter, +# format=get_format(config.get("log_level", "INFO")), +# ) +# show_icon = config.get("log_icon", True) +# lang = Language(get_default_lang_code()) +# +# debug = lang.get("log.debug", default="==DEBUG") +# info = lang.get("log.info", default="===INFO") +# success = lang.get("log.success", default="SUCCESS") +# warning = lang.get("log.warning", default="WARNING") +# error = lang.get("log.error", default="==ERROR") +# +# logger.level("DEBUG", color="", icon=f"{'🐛' if show_icon else ''}{debug}") +# logger.level("INFO", color="", icon=f"{'ℹ️' if show_icon else ''}{info}") +# logger.level("SUCCESS", color="", icon=f"{'✅' if show_icon else ''}{success}") +# logger.level("WARNING", color="", icon=f"{'⚠️' if show_icon else ''}{warning}") +# logger.level("ERROR", color="", icon=f"{'⭕' if show_icon else ''}{error}") diff --git a/tests/test_logger.py b/tests/test_logger.py new file mode 100644 index 000000000..d3322709f --- /dev/null +++ b/tests/test_logger.py @@ -0,0 +1,9 @@ +from liteyuki import logger + +def test_logger(): + logger.info('Hello, World!') + logger.debug('Hello, World!') + logger.warning('Hello, World!') + logger.error('Hello, World!') + logger.critical('Hello, World!') + logger.success("Hello, World!")