2024-03-23 19:55:12 +08:00
|
|
|
|
import sys
|
|
|
|
|
import loguru
|
2024-03-24 10:26:40 +08:00
|
|
|
|
from typing import TYPE_CHECKING
|
2024-03-24 21:21:57 +08:00
|
|
|
|
from .config import load_from_yaml
|
2024-04-03 01:03:25 +08:00
|
|
|
|
from .language import Language, get_default_lang
|
2024-03-23 19:55:12 +08:00
|
|
|
|
|
2024-03-24 10:26:40 +08:00
|
|
|
|
logger = loguru.logger
|
2024-03-23 19:55:12 +08:00
|
|
|
|
if TYPE_CHECKING:
|
2024-03-24 10:26:40 +08:00
|
|
|
|
# avoid sphinx autodoc resolve annotation failed
|
|
|
|
|
# because loguru module do not have `Logger` class actually
|
2024-03-23 19:55:12 +08:00
|
|
|
|
from loguru import Logger, 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
|
|
|
|
|
|
|
|
|
|
|
2024-03-24 10:26:40 +08:00
|
|
|
|
# DEBUG日志格式
|
|
|
|
|
debug_format: str = (
|
2024-03-27 07:13:42 +08:00
|
|
|
|
"<c>{time:YYYY-MM-DD HH:mm:ss}</c> "
|
2024-03-23 19:55:12 +08:00
|
|
|
|
"<lvl>[{level.icon}]</lvl> "
|
2024-03-24 10:26:40 +08:00
|
|
|
|
"<c><{name}.{module}.{function}:{line}></c> "
|
|
|
|
|
"{message}"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 默认日志格式
|
|
|
|
|
default_format: str = (
|
2024-03-27 07:13:42 +08:00
|
|
|
|
"<c>{time:MM-DD HH:mm:ss}</c> "
|
2024-03-24 10:26:40 +08:00
|
|
|
|
"<lvl>[{level.icon}]</lvl> "
|
2024-03-23 19:55:12 +08:00
|
|
|
|
"<c><{name}></c> "
|
|
|
|
|
"{message}"
|
|
|
|
|
)
|
2024-03-24 10:26:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_format(level: str) -> str:
|
|
|
|
|
if level == "DEBUG":
|
|
|
|
|
return debug_format
|
|
|
|
|
else:
|
|
|
|
|
return default_format
|
|
|
|
|
|
|
|
|
|
|
2024-03-24 21:21:57 +08:00
|
|
|
|
logger = loguru.logger.bind()
|
2024-03-23 19:55:12 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-24 21:21:57 +08:00
|
|
|
|
def init_log():
|
2024-04-06 04:23:01 +08:00
|
|
|
|
"""
|
|
|
|
|
在语言加载完成后执行
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2024-03-24 21:21:57 +08:00
|
|
|
|
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)
|
2024-04-03 01:03:25 +08:00
|
|
|
|
lang = get_default_lang()
|
2024-03-24 21:21:57 +08:00
|
|
|
|
|
2024-03-26 17:14:41 +08:00
|
|
|
|
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")
|
2024-03-24 21:21:57 +08:00
|
|
|
|
|
2024-04-05 07:02:18 +08:00
|
|
|
|
logger.level("DEBUG", color="<blue>", icon=f"{'🐛' if show_icon else ''}{debug}")
|
|
|
|
|
logger.level("INFO", color="<normal>", icon=f"{'ℹ️' if show_icon else ''}{info}")
|
2024-03-24 21:21:57 +08:00
|
|
|
|
logger.level("SUCCESS", color="<green>", icon=f"{'✅' if show_icon else ''}{success}")
|
|
|
|
|
logger.level("WARNING", color="<yellow>", icon=f"{'⚠️' if show_icon else ''}{warning}")
|
|
|
|
|
logger.level("ERROR", color="<red>", icon=f"{'⭕' if show_icon else ''}{error}")
|