some color translation
This commit is contained in:
parent
918251c61e
commit
61d041de7e
@ -74,6 +74,16 @@ class BaseFormatter(Options):
|
||||
message = formatter._format(message)
|
||||
return message
|
||||
|
||||
@property
|
||||
def template(self) -> str:
|
||||
return self.default_template
|
||||
|
||||
@template.setter
|
||||
def template(self, template: str) -> None:
|
||||
if not isinstance(template, str):
|
||||
raise TypeError(f'The template must be str, not {type(template)}')
|
||||
self.default_template = template
|
||||
|
||||
|
||||
class TimeFormatter(BaseFormatter):
|
||||
name = 'TimeFormatter'
|
||||
@ -135,14 +145,14 @@ class LevelFormatter(BaseFormatter):
|
||||
else:
|
||||
if self.level_get_higher:
|
||||
for level in self.name_level_map:
|
||||
if message[0].level < self.name_level_map[level]:
|
||||
if message[0].level <= self.name_level_map[level]:
|
||||
level_tag = level
|
||||
break
|
||||
else:
|
||||
level_tag = 'FATAL'
|
||||
else:
|
||||
for level in self.name_level_map:
|
||||
if message[0].level > self.name_level_map[level]:
|
||||
if message[0].level >= self.name_level_map[level]:
|
||||
level_tag = level
|
||||
break
|
||||
else:
|
||||
@ -179,10 +189,16 @@ class StdFormatter(BaseFormatter):
|
||||
LevelFormatter(),
|
||||
TraceFormatter()]
|
||||
|
||||
from lib_not_dr.logger.formatter.colors import LevelColorFormatter, LoggerColorFormatter, TimeColorFormatter
|
||||
from lib_not_dr.logger.formatter.colors import (LevelColorFormatter,
|
||||
LoggerColorFormatter,
|
||||
TimeColorFormatter,
|
||||
TraceColorFormatter,
|
||||
MessageColorFormatter)
|
||||
color_formatters = [LevelColorFormatter(),
|
||||
LoggerColorFormatter(),
|
||||
TimeColorFormatter()]
|
||||
TimeColorFormatter(),
|
||||
TraceColorFormatter(),
|
||||
MessageColorFormatter()]
|
||||
|
||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||
super()._format(message)
|
||||
@ -199,7 +215,7 @@ if __name__ == '__main__':
|
||||
import inspect
|
||||
|
||||
log_message = LogMessage(messages=['Hello World!'],
|
||||
level=10,
|
||||
level=7,
|
||||
stack_trace=inspect.currentframe(),
|
||||
logger_tag='tester',
|
||||
logger_name='test')
|
||||
@ -215,3 +231,8 @@ if __name__ == '__main__':
|
||||
|
||||
print(StdFormatter.info())
|
||||
print(StdFormatter().format_message(log_message))
|
||||
|
||||
test_levels = (0, 2, 5, 7, 10, 30, 50, 90)
|
||||
for test_level in test_levels:
|
||||
log_message.level = test_level
|
||||
print(StdFormatter().format_message(log_message), end='')
|
||||
|
@ -12,9 +12,13 @@ from lib_not_dr.logger.structers import FormattingMessage
|
||||
|
||||
__all__ = [
|
||||
'BaseColorFormatter',
|
||||
|
||||
'LevelColorFormatter',
|
||||
'LoggerColorFormatter',
|
||||
'TimeColorFormatter',
|
||||
'TraceColorFormatter',
|
||||
'MessageColorFormatter',
|
||||
|
||||
'RESET_COLOR'
|
||||
]
|
||||
|
||||
@ -43,14 +47,13 @@ class BaseColorFormatter(BaseFormatter):
|
||||
90: '\033[0;41m'
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_level(cls, message: FormattingMessage) -> int:
|
||||
for level in cls.color:
|
||||
if message[0].level < level:
|
||||
def get_color(self, message: FormattingMessage) -> str:
|
||||
for level in self.color:
|
||||
if message[0].level <= level:
|
||||
break
|
||||
else:
|
||||
level = 90
|
||||
return level
|
||||
return self.color[level]
|
||||
|
||||
|
||||
class LevelColorFormatter(BaseColorFormatter):
|
||||
@ -82,11 +85,11 @@ class LevelColorFormatter(BaseColorFormatter):
|
||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||
if isinstance(message[1].get('level'), int):
|
||||
return message
|
||||
# 向上寻找等级
|
||||
level = self.get_level(message)
|
||||
# 获取颜色
|
||||
color = self.color[level]
|
||||
color = self.get_color(message)
|
||||
# 添加颜色
|
||||
if color == '' or color == RESET_COLOR:
|
||||
return message
|
||||
message[1]['level'] = f'{color}{message[1]["level"]}{RESET_COLOR}'
|
||||
return message
|
||||
|
||||
@ -110,7 +113,7 @@ class LoggerColorFormatter(BaseColorFormatter):
|
||||
# Error: red
|
||||
50: '\033[0;31m',
|
||||
# Fatal: red background
|
||||
90: '\033[0;41m',
|
||||
90: '\033[38;2;245;189;230m',
|
||||
}
|
||||
|
||||
@classmethod
|
||||
@ -120,11 +123,11 @@ class LoggerColorFormatter(BaseColorFormatter):
|
||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||
if message[1].get('logger_name') is None:
|
||||
return message
|
||||
# 向上寻找等级
|
||||
level = self.get_level(message)
|
||||
# 获取颜色
|
||||
color = self.color[level]
|
||||
color = self.get_color(message)
|
||||
# 添加颜色
|
||||
if color == '' or color == RESET_COLOR:
|
||||
return message
|
||||
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') != ' ':
|
||||
message[1]['logger_tag'] = f'{color}{message[1]["logger_tag"]}{RESET_COLOR}'
|
||||
@ -160,10 +163,93 @@ class TimeColorFormatter(BaseColorFormatter):
|
||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||
if message[1].get('log_time') is None:
|
||||
return message
|
||||
# 向上寻找等级
|
||||
level = self.get_level(message)
|
||||
# 获取颜色
|
||||
color = self.color[level]
|
||||
color = self.get_color(message)
|
||||
# 添加颜色
|
||||
if color == '' or color == RESET_COLOR:
|
||||
return message
|
||||
message[1]['log_time'] = f'{color}{message[1]["log_time"]}{RESET_COLOR}'
|
||||
return message
|
||||
|
||||
|
||||
class TraceColorFormatter(BaseColorFormatter):
|
||||
name = 'TraceColorFormatter'
|
||||
# TODO 迁移老 logger 颜色
|
||||
color = {
|
||||
# Notset: just black
|
||||
0: '\033[38;2;0;255;180m',
|
||||
# Trace: blue
|
||||
2: '\033[38;2;0;255;180m',
|
||||
# Fine: green
|
||||
5: '\033[38;2;0;255;180m',
|
||||
# Debug: cyan
|
||||
7: '\033[38;2;0;255;180m',
|
||||
# Info: white
|
||||
10: '\033[38;2;0;255;180m',
|
||||
# Warn: yellow
|
||||
30: '\033[38;2;0;255;180m',
|
||||
# Error: red
|
||||
50: '\033[38;2;0;255;180m',
|
||||
# Fatal: red background
|
||||
90: '\033[38;2;0;255;180m',
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _info(cls) -> str:
|
||||
info = cls.add_info('colored logging file', 'log_source', 'A colored logging file name')
|
||||
info += '\n'
|
||||
info += cls.add_info('colored logging line', 'log_line', 'A colored logging line number')
|
||||
info += '\n'
|
||||
info += cls.add_info('colored logging function', 'log_function', 'A colored logging function name')
|
||||
return info
|
||||
|
||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||
if message[0].stack_trace is None:
|
||||
return message
|
||||
# 获取颜色
|
||||
color = self.get_color(message)
|
||||
# 添加颜色
|
||||
if color == '' or color == RESET_COLOR:
|
||||
return message
|
||||
message[1]['log_source'] = f'{color}{message[1]["log_source"]}{RESET_COLOR}'
|
||||
message[1]['log_line'] = f'{color}{message[1]["log_line"]}{RESET_COLOR}'
|
||||
message[1]['log_function'] = f'{color}{message[1]["log_function"]}{RESET_COLOR}'
|
||||
return message
|
||||
|
||||
|
||||
class MessageColorFormatter(BaseColorFormatter):
|
||||
name = 'MessageColorFormatter'
|
||||
|
||||
color = {
|
||||
# Notset: just black
|
||||
0: '',
|
||||
# Trace: blue
|
||||
2: '\033[38;2;138;173;244m',
|
||||
# Fine: blue
|
||||
5: '\033[38;2;138;173;244m',
|
||||
# Debug: blue
|
||||
7: '\033[38;2;138;173;244m',
|
||||
# Info: no color
|
||||
10: '',
|
||||
# Warn: yellow
|
||||
30: '\033[0;33m',
|
||||
# Error: red
|
||||
50: '\033[0;31m',
|
||||
# Fatal: red background
|
||||
90: '\033[38;2;255;255;0;48;2;120;10;10m',
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _info(cls) -> str:
|
||||
return cls.add_info('colored message', 'message', 'A colored message')
|
||||
|
||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||
if message[1].get('messages') is None:
|
||||
return message
|
||||
# 获取颜色
|
||||
color = self.get_color(message)
|
||||
# 添加颜色
|
||||
if color == '' or color == RESET_COLOR:
|
||||
return message
|
||||
message[1]['messages'] = f'{color}{message[1]["messages"]}{RESET_COLOR}'
|
||||
return message
|
||||
|
@ -13,6 +13,8 @@ from lib_not_dr.logger.structers import LogMessage
|
||||
|
||||
class FormatterTest(unittest.TestCase):
|
||||
|
||||
test_levels = (0, 2, 5, 7, 10, 30, 50, 90)
|
||||
|
||||
def test_create_message(self):
|
||||
message = LogMessage(messages=['test'],
|
||||
level=20,
|
||||
@ -41,6 +43,14 @@ class FormatterTest(unittest.TestCase):
|
||||
def test_level_formatter(self):
|
||||
formatter = LevelFormatter()
|
||||
formatter.info()
|
||||
message = LogMessage(messages=['test'],
|
||||
level=0)
|
||||
|
||||
for test_level in self.test_levels:
|
||||
message.level = test_level
|
||||
basic_info = message.format_for_message()
|
||||
formatting_message = formatter._format((message, basic_info))
|
||||
self.assertEqual(formatting_message[1]['level'], formatter.level_name_map[formatting_message[0].level])
|
||||
|
||||
def test_std_formatter(self):
|
||||
formatter = BaseFormatter()
|
||||
|
Loading…
Reference in New Issue
Block a user