mirror of
https://github.com/Nanaloveyuki/py-logiliteal.git
synced 2025-09-04 02:26:23 +00:00
🐛 修复了颜色插值为空无法渲染的问题
This commit is contained in:
@ -28,5 +28,6 @@
|
|||||||
"WARN": "#fff600",
|
"WARN": "#fff600",
|
||||||
"ERRO": "#ffa000",
|
"ERRO": "#ffa000",
|
||||||
"CRIT": "#ff8181"
|
"CRIT": "#ff8181"
|
||||||
}
|
},
|
||||||
|
"time_color": "#28ffb6"
|
||||||
}
|
}
|
@ -33,6 +33,7 @@ DEFAULT_CONFIG = {
|
|||||||
"weekday_format": "%A",
|
"weekday_format": "%A",
|
||||||
"level_name": {"DEBUG": "DEBUG", "INFO": "INFO", "WARN": "WARN", "ERRO": "ERRO", "CRIT": "CRIT"},
|
"level_name": {"DEBUG": "DEBUG", "INFO": "INFO", "WARN": "WARN", "ERRO": "ERRO", "CRIT": "CRIT"},
|
||||||
"level_color": {"DEBUG": "#c1d5ff", "INFO": "#c1ffff", "WARN": "#fff600", "ERRO": "#ffa000", "CRIT": "#ff8181"},
|
"level_color": {"DEBUG": "#c1d5ff", "INFO": "#c1ffff", "WARN": "#fff600", "ERRO": "#ffa000", "CRIT": "#ff8181"},
|
||||||
|
"time_color": "#28ffb6",
|
||||||
}
|
}
|
||||||
|
|
||||||
g_config_cache = None
|
g_config_cache = None
|
||||||
|
@ -12,6 +12,8 @@ from .time import get_asctime, get_time, get_weekday, get_date
|
|||||||
from .styles import set_color, set_bg_color
|
from .styles import set_color, set_bg_color
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
time_color = get_config("time_color")
|
||||||
|
|
||||||
def fmt_level(level: str) -> int:
|
def fmt_level(level: str) -> int:
|
||||||
"""
|
"""
|
||||||
格式化日志级别
|
格式化日志级别
|
||||||
@ -64,10 +66,10 @@ def fmt_placeholder(message: Any, use_date_color: bool = True) -> str:
|
|||||||
message = str(message)
|
message = str(message)
|
||||||
if use_date_color:
|
if use_date_color:
|
||||||
message = message.format_map(SafeDict(
|
message = message.format_map(SafeDict(
|
||||||
asctime = set_color(get_asctime(),"#28ffb6"),
|
asctime = set_color(get_asctime(),time_color),
|
||||||
time = set_color(get_time(),"#28ffb6"),
|
time = set_color(get_time(),time_color),
|
||||||
weekday = set_color(get_weekday(),"#28ffb6"),
|
weekday = set_color(get_weekday(),time_color),
|
||||||
date = set_color(get_date(),"#28ffb6")
|
date = set_color(get_date(),time_color)
|
||||||
))
|
))
|
||||||
else:
|
else:
|
||||||
message = message.format_map(SafeDict(
|
message = message.format_map(SafeDict(
|
||||||
|
@ -8,19 +8,21 @@ py-logiliteal's style tools, used to format log output
|
|||||||
|
|
||||||
from typing import Union, Optional
|
from typing import Union, Optional
|
||||||
|
|
||||||
def _get_hex_to_ansi(hex_color: str) -> Union[Optional[str], None]:
|
def _get_hex_to_ansi(hex_color: str|None) -> Union[Optional[str], None]:
|
||||||
"""
|
"""
|
||||||
将16进制颜色值转换为ANSI转义序列
|
将16进制颜色值转换为ANSI转义序列
|
||||||
Convert hex color value to ANSI escape sequence
|
Convert hex color value to ANSI escape sequence
|
||||||
:param hex_color: 16进制颜色值 Hex color value
|
:param hex_color: 16进制颜色值 Hex color value
|
||||||
:return: ANSI转义序列 ANSI escape sequence
|
:return: ANSI转义序列 ANSI escape sequence
|
||||||
"""
|
"""
|
||||||
|
if not hex_color:
|
||||||
|
return ""
|
||||||
if not hex_color.startswith("#"):
|
if not hex_color.startswith("#"):
|
||||||
return None
|
return None
|
||||||
r, g, b = int(hex_color[1:3], 16), int(hex_color[3:5], 16), int(hex_color[5:7], 16)
|
r, g, b = int(hex_color[1:3], 16), int(hex_color[3:5], 16), int(hex_color[5:7], 16)
|
||||||
return f"\033[38;2;{r};{g};{b}m"
|
return f"\033[38;2;{r};{g};{b}m"
|
||||||
|
|
||||||
def set_color(text: str, color: str) -> str:
|
def set_color(text: str|None, color: str|None) -> str:
|
||||||
"""
|
"""
|
||||||
设置文本颜色
|
设置文本颜色
|
||||||
Set text color
|
Set text color
|
||||||
@ -28,12 +30,16 @@ def set_color(text: str, color: str) -> str:
|
|||||||
:param color: 颜色值 Color value
|
:param color: 颜色值 Color value
|
||||||
:return: 格式化后的文本 Formatted text
|
:return: 格式化后的文本 Formatted text
|
||||||
"""
|
"""
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
if not color:
|
||||||
|
return text
|
||||||
ansi = _get_hex_to_ansi(color)
|
ansi = _get_hex_to_ansi(color)
|
||||||
if not ansi:
|
if not ansi:
|
||||||
return text
|
return text
|
||||||
return f"{ansi}{text}\033[0m"
|
return f"{ansi}{text}\033[0m"
|
||||||
|
|
||||||
def set_bg_color(text: str, color: str) -> str:
|
def set_bg_color(text: str|None, color: str|None) -> str:
|
||||||
"""
|
"""
|
||||||
设置文本背景颜色
|
设置文本背景颜色
|
||||||
Set text background color
|
Set text background color
|
||||||
@ -41,14 +47,17 @@ def set_bg_color(text: str, color: str) -> str:
|
|||||||
:param color: 颜色值 Color value
|
:param color: 颜色值 Color value
|
||||||
:return: 格式化后的文本 Formatted text
|
:return: 格式化后的文本 Formatted text
|
||||||
"""
|
"""
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
if not color:
|
||||||
|
return text
|
||||||
ansi = _get_hex_to_ansi(color)
|
ansi = _get_hex_to_ansi(color)
|
||||||
if not ansi:
|
if not ansi:
|
||||||
return text
|
return text
|
||||||
# 将前景色ANSI代码转换为背景色代码 (38→48)
|
|
||||||
ansi = ansi.replace("38;", "48;")
|
ansi = ansi.replace("38;", "48;")
|
||||||
return f"{ansi}{text}\033[0m"
|
return f"{ansi}{text}\033[0m"
|
||||||
|
|
||||||
def set_style(text: str, bold: bool = False, underline: bool = False, reverse: bool = False) -> str:
|
def set_style(text: str|None, bold: bool = False, underline: bool = False, reverse: bool = False) -> str:
|
||||||
"""
|
"""
|
||||||
设置文本样式
|
设置文本样式
|
||||||
Set text style
|
Set text style
|
||||||
@ -58,6 +67,8 @@ def set_style(text: str, bold: bool = False, underline: bool = False, reverse: b
|
|||||||
:param reverse: 是否反相 Is reverse
|
:param reverse: 是否反相 Is reverse
|
||||||
:return: 格式化后的文本 Formatted text
|
:return: 格式化后的文本 Formatted text
|
||||||
"""
|
"""
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
ansi = ""
|
ansi = ""
|
||||||
if bold:
|
if bold:
|
||||||
ansi += "\033[1m"
|
ansi += "\033[1m"
|
||||||
|
@ -4,7 +4,7 @@ from pathlib import Path
|
|||||||
project_root = Path(__file__).parent.parent
|
project_root = Path(__file__).parent.parent
|
||||||
sys.path.append(str(project_root))
|
sys.path.append(str(project_root))
|
||||||
|
|
||||||
from src.logiliteal.utils.configs import get_config, set_config, reset_config
|
from src.logiliteal.utils.configs import get_config, set_config, reset_config, create_backup
|
||||||
|
|
||||||
print(f"\n配置更换测试")
|
print(f"\n配置更换测试")
|
||||||
set_config("file_level", "DEBUG")
|
set_config("file_level", "DEBUG")
|
||||||
|
Reference in New Issue
Block a user