From 788bca711315142b0394de41595bdc6cd553da7a Mon Sep 17 00:00:00 2001 From: snowy Date: Sun, 24 Mar 2024 21:21:57 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++-- liteyuki/liteyuki_main/loader.py | 3 ++ liteyuki/resources/lang/zh-Kawaii.lang | 8 ++--- liteyuki/utils/__init__.py | 10 +++--- liteyuki/utils/config.py | 4 +-- liteyuki/utils/language.py | 14 ++++---- liteyuki/utils/log.py | 46 +++++++++++++++++--------- main.py | 2 +- 8 files changed, 56 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 8fc64894..1acb61d0 100644 --- a/README.md +++ b/README.md @@ -32,16 +32,15 @@ ### 轻雪配置项(Nonebot插件配置项也可以写在此) ```yaml -# 建议修改的配置项目 +# 配置项,如果不确定字段的含义,请不要修改 command_start: [ "/", " " ] # 指令前缀 host: 127.0.0.1 # 反向监听地址 port: 20216 # 绑定端口 nickname: [ "liteyuki" ] # 机器人昵称 superusers: [ "1919810" ] # 超级用户 -show_icon: true # 是否显示日志等级图标(某些控制台不可用) +default_language: "zh-CN" # 默认语言 +log_icon: true # 是否显示日志等级图标(某些控制台不可用) auto_report: true # 是否自动上报设备信息给轻雪服务器,该信息仅包含硬件信息和运行软件版本 - -# 下面是不建议修改,且默认没有列出的配置项,除非你有特殊需求 log_level: "INFO" # 日志等级 # 其他Nonebot插件的配置项 diff --git a/liteyuki/liteyuki_main/loader.py b/liteyuki/liteyuki_main/loader.py index 9c2a3af3..c21b419f 100644 --- a/liteyuki/liteyuki_main/loader.py +++ b/liteyuki/liteyuki_main/loader.py @@ -2,6 +2,7 @@ import os import nonebot.plugin +from liteyuki.utils import init_log from liteyuki.utils.data_manager import InstalledPlugin, plugin_db from liteyuki.utils.resource import load_resource_from_dir from liteyuki.utils.tools import check_for_package @@ -13,6 +14,8 @@ load_resource_from_dir(RESOURCE_PATH) nonebot.plugin.load_plugins("liteyuki/plugins") nonebot.plugin.load_plugins("plugins") +init_log() + installed_plugins = plugin_db.all(InstalledPlugin) if installed_plugins: for installed_plugin in plugin_db.all(InstalledPlugin): diff --git a/liteyuki/resources/lang/zh-Kawaii.lang b/liteyuki/resources/lang/zh-Kawaii.lang index 40712e71..638b6bf6 100644 --- a/liteyuki/resources/lang/zh-Kawaii.lang +++ b/liteyuki/resources/lang/zh-Kawaii.lang @@ -1,9 +1,9 @@ language.name=简体中文(轻雪版) -log.debug=调试 -log.info=信息 -log.warning=有问题哦 -log.error=出错啦 +log.debug=调试中 +log.info=是信息 +log.warning=有问题 +log.error=出错辣 log.success=成功啦 main.current_language=现在系统用的语言是:{LANG} 喔! diff --git a/liteyuki/utils/__init__.py b/liteyuki/utils/__init__.py index b4b773ed..2633dc77 100644 --- a/liteyuki/utils/__init__.py +++ b/liteyuki/utils/__init__.py @@ -10,7 +10,8 @@ __VERSION__ = "6.2.4" # 60201 import requests -from liteyuki.utils.config import load_from_yaml +from liteyuki.utils.config import load_from_yaml, config +from .log import init_log major, minor, patch = map(int, __VERSION__.split(".")) __VERSION_I__ = major * 10000 + minor * 100 + patch @@ -51,9 +52,9 @@ def init(): if sys.version_info < (3, 10): nonebot.logger.error("This project requires Python3.10+ to run, please upgrade your Python Environment.") exit(1) - - load_from_yaml("config.yml") - from .log import logger + # 在加载完成语言后再初始化日志 + init_log() + nonebot.logger.info("Liteyuki is initializing...") if not os.path.exists("data/liteyuki/liteyuki.json"): register_bot() @@ -73,5 +74,4 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ f"Run Liteyuki with Python{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} " f"at {sys.executable}" ) - nonebot.logger.info(f"{__NAME__} {__VERSION__}({__VERSION_I__}) is running") diff --git a/liteyuki/utils/config.py b/liteyuki/utils/config.py index 73ce31ca..c74ade33 100644 --- a/liteyuki/utils/config.py +++ b/liteyuki/utils/config.py @@ -4,7 +4,7 @@ import nonebot import yaml from pydantic import BaseModel -config = {} +config = {} # 全局配置 class BasicConfig(BaseModel): @@ -16,8 +16,8 @@ class BasicConfig(BaseModel): def load_from_yaml(file: str) -> dict: - nonebot.logger.debug("Loading config from %s" % file) global config + nonebot.logger.debug("Loading config from %s" % file) if not os.path.exists(file): nonebot.logger.warning(f'Config file {file} not found, created default config, please modify it and restart') with open(file, 'w', encoding='utf-8') as f: diff --git a/liteyuki/utils/language.py b/liteyuki/utils/language.py index 130ce255..54221a14 100644 --- a/liteyuki/utils/language.py +++ b/liteyuki/utils/language.py @@ -9,8 +9,8 @@ from typing import Any import nonebot -from liteyuki.utils.config import config -from liteyuki.utils.data_manager import User, user_db +from .config import config +from .data_manager import User, user_db _default_lang_code = "en" _language_data = { @@ -62,7 +62,6 @@ def load_from_json(file_path: str, lang_code: str = None): if lang_code not in _language_data: _language_data[lang_code] = {} _language_data[lang_code].update(data) - nonebot.logger.debug(f"Loaded language data from {file_path}") except Exception as e: nonebot.logger.error(f"Failed to load language data from {file_path}: {e}") @@ -113,20 +112,23 @@ class Language: Args: item: 文本键 *args: 格式化参数 + **kwargs: 格式化参数 Returns: str: 当前语言的文本 """ + default = kwargs.pop("default", None) + try: if self.lang_code in _language_data and item in _language_data[self.lang_code]: return _language_data[self.lang_code][item].format(*args, **kwargs) if self.fallback_lang_code in _language_data and item in _language_data[self.fallback_lang_code]: return _language_data[self.fallback_lang_code][item].format(*args, **kwargs) - return item + return default or item except Exception as e: nonebot.logger.error(f"Failed to get language text or format: {e}") - return item + return default or item def get_user_lang(user_id: str) -> Language: @@ -150,7 +152,7 @@ def get_system_lang_code() -> str: def get_default_lang() -> Language: """ - 获取默认/系统语言 + 获取配置默认/系统语言 """ return Language(config.get("default_language", get_system_lang_code())) diff --git a/liteyuki/utils/log.py b/liteyuki/utils/log.py index 1d5610f7..af18f821 100644 --- a/liteyuki/utils/log.py +++ b/liteyuki/utils/log.py @@ -1,7 +1,8 @@ import sys import loguru from typing import TYPE_CHECKING -from .config import config, load_from_yaml +from .config import load_from_yaml +from .language import Language, get_default_lang logger = loguru.logger if TYPE_CHECKING: @@ -41,20 +42,33 @@ def get_format(level: str) -> str: return default_format -logger = loguru.logger.bind(get_format=get_format) +logger = loguru.logger.bind() -logger.remove() -logger_id = logger.add( - sys.stdout, - level=0, - diagnose=False, - filter=default_filter, - format=get_format(config.get("log_level", "INFO")), -) -show_icon = config.get("show_icon", True) -logger.level("DEBUG", color="", icon=f"*️⃣==DEBUG") -logger.level("INFO", color="", icon=f"ℹ️===INFO") -logger.level("SUCCESS", color="", icon=f"✅SUCCESS") -logger.level("WARNING", color="", icon=f"⚠️WARNING") -logger.level("ERROR", color="", icon=f"⭕==ERROR") +def init_log(): + 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(config.get("default_language", "en")) + + 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/main.py b/main.py index d43b5abd..c1798ae6 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import nonebot from nonebot.adapters.onebot import v11, v12 -from liteyuki.utils.config import load_from_yaml +from liteyuki.utils.config import config, load_from_yaml from liteyuki.utils import init init()