Looks better

This commit is contained in:
shenjack 2023-11-02 00:09:45 +08:00
parent d42491a95c
commit b61f3a450d
Signed by: shenjack
GPG Key ID: 7B1134A979775551

View File

@ -23,9 +23,7 @@ RESET_COLOR = '\033[0m'
class BaseColorFormatter(BaseFormatter): class BaseColorFormatter(BaseFormatter):
name = 'BaseColorFormatter' name = 'BaseColorFormatter'
@staticmethod color = {
def _default_color() -> Dict[int, str]:
return {
# Notset: just black # Notset: just black
0: '', 0: '',
# Trace: blue # Trace: blue
@ -42,13 +40,11 @@ class BaseColorFormatter(BaseFormatter):
50: '\033[0;31m', 50: '\033[0;31m',
# Fatal: red background # Fatal: red background
90: '\033[0;41m' 90: '\033[0;41m'
} }
@staticmethod @classmethod
def get_level(message: FormattingMessage, colors: Dict[int, str] = None) -> int: def get_level(cls, message: FormattingMessage) -> int:
if colors is None: for level in cls.color:
colors = BaseColorFormatter._default_color()
for level in colors:
if message[0].level < level: if message[0].level < level:
break break
else: else:
@ -88,9 +84,9 @@ class LevelColorFormatter(BaseColorFormatter):
if isinstance(message[1].get('level'), int): if isinstance(message[1].get('level'), int):
return message return message
# 向上寻找等级 # 向上寻找等级
level = self.get_level(message, self._default_color()) level = self.get_level(message)
# 获取颜色 # 获取颜色
color = self._default_color()[level] color = self.color[level]
# 添加颜色 # 添加颜色
message[1]['level'] = f'{color}{message[1]["level"]}{RESET_COLOR}' message[1]['level'] = f'{color}{message[1]["level"]}{RESET_COLOR}'
return message return message
@ -128,9 +124,9 @@ class LoggerColorFormatter(BaseColorFormatter):
if message[1].get('logger_name') is None: if message[1].get('logger_name') is None:
return message return message
# 向上寻找等级 # 向上寻找等级
level = self.get_level(message, self._default_color()) level = self.get_level(message)
# 获取颜色 # 获取颜色
color = self._default_color()[level] color = self.color[level]
# 添加颜色 # 添加颜色
message[1]['logger_name'] = f'{color}{message[1]["logger_name"]}{RESET_COLOR}' message[1]['logger_name'] = f'{color}{message[1]["logger_name"]}{RESET_COLOR}'
if message[1].get('logger_tag') is not None and message[1].get('logger_tag') != ' ': if message[1].get('logger_tag') is not None and message[1].get('logger_tag') != ' ':
@ -141,9 +137,7 @@ class LoggerColorFormatter(BaseColorFormatter):
class TimeColorFormatter(BaseColorFormatter): class TimeColorFormatter(BaseColorFormatter):
name = 'TimeColorFormatter' name = 'TimeColorFormatter'
@staticmethod color = {
def _default_color() -> Dict[int, str]:
return {
# Notset: just black # Notset: just black
0: '', 0: '',
# Trace: blue # Trace: blue
@ -160,7 +154,7 @@ class TimeColorFormatter(BaseColorFormatter):
50: '\033[0;31m', 50: '\033[0;31m',
# Fatal: red background # Fatal: red background
90: '\033[0;41m', 90: '\033[0;41m',
} }
@classmethod @classmethod
def _info(cls) -> str: def _info(cls) -> str:
@ -170,9 +164,9 @@ class TimeColorFormatter(BaseColorFormatter):
if message[1].get('log_time') is None: if message[1].get('log_time') is None:
return message return message
# 向上寻找等级 # 向上寻找等级
level = self.get_level(message, self._default_color()) level = self.get_level(message)
# 获取颜色 # 获取颜色
color = self._default_color()[level] color = self.color[level]
# 添加颜色 # 添加颜色
message[1]['log_time'] = f'{color}{message[1]["log_time"]}{RESET_COLOR}' message[1]['log_time'] = f'{color}{message[1]["log_time"]}{RESET_COLOR}'
return message return message