mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-11-25 00:25:04 +08:00
feat: log样式在不同等级下的支持
This commit is contained in:
parent
66e18a9c8d
commit
c6f65d544f
@ -20,7 +20,8 @@ def init():
|
||||
nonebot.logger.error("This project requires Python3.10+ to run, please upgrade your Python Environment.")
|
||||
exit(1)
|
||||
|
||||
print("\033[34m" + r""" __ ______ ________ ________ __ __ __ __ __ __ ______
|
||||
print("\033[34m" + r"""
|
||||
__ ______ ________ ________ __ __ __ __ __ __ ______
|
||||
/ | / |/ |/ |/ \ / |/ | / |/ | / |/ |
|
||||
$$ | $$$$$$/ $$$$$$$$/ $$$$$$$$/ $$ \ /$$/ $$ | $$ |$$ | /$$/ $$$$$$/
|
||||
$$ | $$ | $$ | $$ |__ $$ \/$$/ $$ | $$ |$$ |/$$/ $$ |
|
||||
@ -28,7 +29,8 @@ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |$$ $$< $$ |
|
||||
$$ | $$ | $$ | $$$$$/ $$$$/ $$ | $$ |$$$$$ \ $$ |
|
||||
$$ |_____ _$$ |_ $$ | $$ |_____ $$ | $$ \__$$ |$$ |$$ \ _$$ |_
|
||||
$$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ |
|
||||
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ """ + "\033[0m")
|
||||
$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/
|
||||
""" + "\033[0m")
|
||||
nonebot.logger.info(
|
||||
f"Run Liteyuki with Python{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} "
|
||||
f"at {sys.executable}"
|
||||
|
@ -116,6 +116,7 @@ class Database(BaseORMAdapter):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
nonebot.logger.warning("Database.auto_migrate is deprecated, use Database.migrate instead")
|
||||
table_name = ''
|
||||
for model in args:
|
||||
model: type(LiteModel)
|
||||
|
@ -1,33 +1,14 @@
|
||||
import sys
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
from .language import get_default_lang
|
||||
import loguru
|
||||
from typing import TYPE_CHECKING
|
||||
from .config import config, load_from_yaml
|
||||
|
||||
logger = loguru.logger
|
||||
if TYPE_CHECKING:
|
||||
# avoid sphinx autodoc resolve annotation failed
|
||||
# because loguru module do not have `Logger` class actually
|
||||
from loguru import Logger, Record
|
||||
|
||||
logger: "Logger" = loguru.logger
|
||||
|
||||
|
||||
class LoguruHandler(logging.Handler): # pragma: no cover
|
||||
"""logging 与 loguru 之间的桥梁,将 logging 的日志转发到 loguru。"""
|
||||
|
||||
def emit(self, record: logging.LogRecord):
|
||||
try:
|
||||
level = logger.level(record.levelname).name
|
||||
except ValueError:
|
||||
level = record.levelno
|
||||
|
||||
frame, depth = sys._getframe(6), 6
|
||||
while frame and frame.f_code.co_filename == logging.__file__:
|
||||
frame = frame.f_back
|
||||
depth += 1
|
||||
|
||||
logger.opt(depth=depth, exception=record.exc_info).log(
|
||||
level, record.getMessage()
|
||||
)
|
||||
|
||||
|
||||
def default_filter(record: "Record"):
|
||||
"""默认的日志过滤器,根据 `config.log_level` 配置改变日志等级。"""
|
||||
@ -36,13 +17,31 @@ def default_filter(record: "Record"):
|
||||
return record["level"].no >= levelno
|
||||
|
||||
|
||||
default_format: str = (
|
||||
# DEBUG日志格式
|
||||
debug_format: str = (
|
||||
"<c>{time:YYYY-MM-DD}</c> <blue>{time:HH:mm:ss}</blue> "
|
||||
"<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> "
|
||||
"<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)
|
||||
|
||||
logger.remove()
|
||||
logger_id = logger.add(
|
||||
@ -50,17 +49,12 @@ logger_id = logger.add(
|
||||
level=0,
|
||||
diagnose=False,
|
||||
filter=default_filter,
|
||||
format=default_format,
|
||||
format=get_format(load_from_yaml('config.yml').get("log_level", "INFO")),
|
||||
)
|
||||
slang = get_default_lang()
|
||||
logger.level("DEBUG", color="<blue>", icon=f"*️⃣ DDDEBUG")
|
||||
logger.level("INFO", color="<white>", icon=f"ℹ️ IIIINFO")
|
||||
logger.level("SUCCESS", color="<green>", icon=f"✅ SUCCESS")
|
||||
logger.level("WARNING", color="<yellow>", icon=f"⚠️ WARNING")
|
||||
logger.level("ERROR", color="<red>", icon=f"⭕ EEERROR")
|
||||
|
||||
"""默认日志处理器 id"""
|
||||
|
||||
__autodoc__ = {
|
||||
"logger_id": False
|
||||
}
|
||||
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")
|
||||
|
Loading…
Reference in New Issue
Block a user