app/liteyuki/utils/log.py

61 lines
1.7 KiB
Python
Raw Normal View History

2024-03-23 19:55:12 +08:00
import sys
import loguru
from typing import TYPE_CHECKING
from .config import config, load_from_yaml
2024-03-23 19:55:12 +08:00
logger = loguru.logger
2024-03-23 19:55:12 +08:00
if TYPE_CHECKING:
# 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
# DEBUG日志格式
debug_format: str = (
2024-03-24 09:43:34 +08:00
"<c>{time:YYYY-MM-DD}</c> <blue>{time:HH:mm:ss}</blue> "
2024-03-23 19:55:12 +08:00
"<lvl>[{level.icon}]</lvl> "
"<c><{name}.{module}.{function}:{line}></c> "
"{message}"
)
# 默认日志格式
default_format: str = (
"<c>{time:MM-DD}</c> <blue>{time:HH:mm:ss}</blue> "
"<lvl>[{level.icon}]</lvl> "
2024-03-23 19:55:12 +08:00
"<c><{name}></c> "
"{message}"
)
def get_format(level: str) -> str:
if level == "DEBUG":
return debug_format
else:
return default_format
logger = loguru.logger.bind(get_format=get_format)
2024-03-23 19:55:12 +08:00
logger.remove()
logger_id = logger.add(
sys.stdout,
level=0,
diagnose=False,
filter=default_filter,
format=get_format(load_from_yaml('config.yml').get("log_level", "INFO")),
2024-03-23 19:55:12 +08:00
)
logger.level("DEBUG", color="<blue>", icon=f"*️⃣==DEBUG")
logger.level("INFO", color="<white>", icon=f"===INFO")
logger.level("SUCCESS", color="<green>", icon=f"✅SUCCESS")
logger.level("WARNING", color="<yellow>", icon=f"WARNING")
logger.level("ERROR", color="<red>", icon=f"⭕==ERROR")