2024-07-27 10:12:45 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
|
|
|
|
|
|
|
|
|
@Time : 2024/7/27 上午9:12
|
|
|
|
|
@Author : snowykami
|
|
|
|
|
@Email : snowykami@outlook.com
|
|
|
|
|
@File : log.py
|
|
|
|
|
@Software: PyCharm
|
|
|
|
|
"""
|
|
|
|
|
import sys
|
|
|
|
|
|
2024-08-19 23:47:39 +08:00
|
|
|
|
import loguru
|
|
|
|
|
|
2024-10-20 20:59:30 +08:00
|
|
|
|
logger = loguru.logger.bind()
|
2024-07-27 10:12:45 +08:00
|
|
|
|
|
|
|
|
|
debug_format: str = (
|
2024-10-20 20:59:30 +08:00
|
|
|
|
"<c>{time:YYYY-MM-DD HH:mm:ss}</c> "
|
|
|
|
|
"<lvl>[{level.icon}{level}]</lvl> "
|
|
|
|
|
"<c><{name}.{module}.{function}:{line}></c> "
|
|
|
|
|
"{message}"
|
2024-07-27 10:12:45 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# 默认日志格式
|
|
|
|
|
default_format: str = (
|
2024-10-20 20:59:30 +08:00
|
|
|
|
"<c>{time:MM-DD HH:mm:ss}</c> "
|
|
|
|
|
"<lvl>[{level.icon}{level}]</lvl> "
|
|
|
|
|
"<c><{name}></c> "
|
|
|
|
|
"{message}"
|
2024-07-27 10:12:45 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_format(level: str) -> str:
|
2024-10-20 20:59:30 +08:00
|
|
|
|
"""
|
|
|
|
|
获取日志格式
|
|
|
|
|
Args:
|
|
|
|
|
level: 日志等级
|
|
|
|
|
|
|
|
|
|
Returns: 日志格式
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# DEBUG日志格式
|
|
|
|
|
|
2024-07-27 10:12:45 +08:00
|
|
|
|
if level == "DEBUG":
|
|
|
|
|
return debug_format
|
|
|
|
|
else:
|
|
|
|
|
return default_format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def init_log(config: dict):
|
|
|
|
|
"""
|
|
|
|
|
在语言加载完成后执行
|
2024-10-20 20:59:30 +08:00
|
|
|
|
Args:
|
|
|
|
|
config: 配置
|
2024-07-27 10:12:45 +08:00
|
|
|
|
"""
|
2024-10-20 20:59:30 +08:00
|
|
|
|
global logger
|
|
|
|
|
level = config.get("log_level", "DEBUG")
|
2024-07-27 10:12:45 +08:00
|
|
|
|
logger.remove()
|
|
|
|
|
logger.add(
|
|
|
|
|
sys.stdout,
|
2024-10-20 20:59:30 +08:00
|
|
|
|
level=level,
|
2024-07-27 10:12:45 +08:00
|
|
|
|
diagnose=False,
|
2024-10-20 20:59:30 +08:00
|
|
|
|
format=get_format(level),
|
2024-07-27 10:12:45 +08:00
|
|
|
|
)
|
|
|
|
|
show_icon = config.get("log_icon", True)
|
2024-10-20 20:59:30 +08:00
|
|
|
|
logger.level("DEBUG", color="<blue>", icon=f"{'🐛' if show_icon else ''}")
|
|
|
|
|
logger.level("INFO", color="<normal>", icon=f"{'ℹ️' if show_icon else ''}")
|
|
|
|
|
logger.level("SUCCESS", color="<green>", icon=f"{'✅' if show_icon else ''}")
|
|
|
|
|
logger.level("WARNING", color="<yellow>", icon=f"{'⚠️' if show_icon else ''}")
|
|
|
|
|
logger.level("ERROR", color="<red>", icon=f"{'⭕' if show_icon else ''}")
|
|
|
|
|
logger.level("CRITICAL", color="<red>", icon=f"{'❌' if show_icon else ''}")
|
|
|
|
|
logger.level("TRACE", color="<cyan>", icon=f"{'🔍' if show_icon else ''}")
|
2024-08-12 04:45:59 +08:00
|
|
|
|
|
2024-10-20 20:59:30 +08:00
|
|
|
|
logger.bind()
|
2024-07-27 10:12:45 +08:00
|
|
|
|
|
2024-10-20 20:59:30 +08:00
|
|
|
|
init_log(config={"log_level": "DEBUG", "log_icon": True})
|