some color translation
This commit is contained in:
parent
918251c61e
commit
61d041de7e
@ -74,6 +74,16 @@ class BaseFormatter(Options):
|
|||||||
message = formatter._format(message)
|
message = formatter._format(message)
|
||||||
return 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):
|
class TimeFormatter(BaseFormatter):
|
||||||
name = 'TimeFormatter'
|
name = 'TimeFormatter'
|
||||||
@ -135,14 +145,14 @@ class LevelFormatter(BaseFormatter):
|
|||||||
else:
|
else:
|
||||||
if self.level_get_higher:
|
if self.level_get_higher:
|
||||||
for level in self.name_level_map:
|
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
|
level_tag = level
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
level_tag = 'FATAL'
|
level_tag = 'FATAL'
|
||||||
else:
|
else:
|
||||||
for level in self.name_level_map:
|
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
|
level_tag = level
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
@ -179,10 +189,16 @@ class StdFormatter(BaseFormatter):
|
|||||||
LevelFormatter(),
|
LevelFormatter(),
|
||||||
TraceFormatter()]
|
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(),
|
color_formatters = [LevelColorFormatter(),
|
||||||
LoggerColorFormatter(),
|
LoggerColorFormatter(),
|
||||||
TimeColorFormatter()]
|
TimeColorFormatter(),
|
||||||
|
TraceColorFormatter(),
|
||||||
|
MessageColorFormatter()]
|
||||||
|
|
||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||||
super()._format(message)
|
super()._format(message)
|
||||||
@ -199,7 +215,7 @@ if __name__ == '__main__':
|
|||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
log_message = LogMessage(messages=['Hello World!'],
|
log_message = LogMessage(messages=['Hello World!'],
|
||||||
level=10,
|
level=7,
|
||||||
stack_trace=inspect.currentframe(),
|
stack_trace=inspect.currentframe(),
|
||||||
logger_tag='tester',
|
logger_tag='tester',
|
||||||
logger_name='test')
|
logger_name='test')
|
||||||
@ -215,3 +231,8 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
print(StdFormatter.info())
|
print(StdFormatter.info())
|
||||||
print(StdFormatter().format_message(log_message))
|
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__ = [
|
__all__ = [
|
||||||
'BaseColorFormatter',
|
'BaseColorFormatter',
|
||||||
|
|
||||||
'LevelColorFormatter',
|
'LevelColorFormatter',
|
||||||
'LoggerColorFormatter',
|
'LoggerColorFormatter',
|
||||||
'TimeColorFormatter',
|
'TimeColorFormatter',
|
||||||
|
'TraceColorFormatter',
|
||||||
|
'MessageColorFormatter',
|
||||||
|
|
||||||
'RESET_COLOR'
|
'RESET_COLOR'
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -43,14 +47,13 @@ class BaseColorFormatter(BaseFormatter):
|
|||||||
90: '\033[0;41m'
|
90: '\033[0;41m'
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
def get_color(self, message: FormattingMessage) -> str:
|
||||||
def get_level(cls, message: FormattingMessage) -> int:
|
for level in self.color:
|
||||||
for level in cls.color:
|
if message[0].level <= level:
|
||||||
if message[0].level < level:
|
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
level = 90
|
level = 90
|
||||||
return level
|
return self.color[level]
|
||||||
|
|
||||||
|
|
||||||
class LevelColorFormatter(BaseColorFormatter):
|
class LevelColorFormatter(BaseColorFormatter):
|
||||||
@ -82,11 +85,11 @@ class LevelColorFormatter(BaseColorFormatter):
|
|||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||||
if isinstance(message[1].get('level'), int):
|
if isinstance(message[1].get('level'), int):
|
||||||
return message
|
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}'
|
message[1]['level'] = f'{color}{message[1]["level"]}{RESET_COLOR}'
|
||||||
return message
|
return message
|
||||||
|
|
||||||
@ -110,7 +113,7 @@ class LoggerColorFormatter(BaseColorFormatter):
|
|||||||
# Error: red
|
# Error: red
|
||||||
50: '\033[0;31m',
|
50: '\033[0;31m',
|
||||||
# Fatal: red background
|
# Fatal: red background
|
||||||
90: '\033[0;41m',
|
90: '\033[38;2;245;189;230m',
|
||||||
}
|
}
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -120,11 +123,11 @@ class LoggerColorFormatter(BaseColorFormatter):
|
|||||||
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||||
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)
|
|
||||||
# 获取颜色
|
# 获取颜色
|
||||||
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}'
|
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') != ' ':
|
||||||
message[1]['logger_tag'] = f'{color}{message[1]["logger_tag"]}{RESET_COLOR}'
|
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:
|
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||||
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)
|
|
||||||
# 获取颜色
|
# 获取颜色
|
||||||
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}'
|
message[1]['log_time'] = f'{color}{message[1]["log_time"]}{RESET_COLOR}'
|
||||||
return message
|
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):
|
class FormatterTest(unittest.TestCase):
|
||||||
|
|
||||||
|
test_levels = (0, 2, 5, 7, 10, 30, 50, 90)
|
||||||
|
|
||||||
def test_create_message(self):
|
def test_create_message(self):
|
||||||
message = LogMessage(messages=['test'],
|
message = LogMessage(messages=['test'],
|
||||||
level=20,
|
level=20,
|
||||||
@ -41,6 +43,14 @@ class FormatterTest(unittest.TestCase):
|
|||||||
def test_level_formatter(self):
|
def test_level_formatter(self):
|
||||||
formatter = LevelFormatter()
|
formatter = LevelFormatter()
|
||||||
formatter.info()
|
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):
|
def test_std_formatter(self):
|
||||||
formatter = BaseFormatter()
|
formatter = BaseFormatter()
|
||||||
|
Loading…
Reference in New Issue
Block a user