Compare commits

...

2 Commits

Author SHA1 Message Date
bf1ae6a5ab
Fixed 2023-10-25 06:21:19 +08:00
767bfbd81d
Need more test 2023-10-25 06:20:26 +08:00
2 changed files with 30 additions and 6 deletions

View File

@ -25,12 +25,18 @@ class BaseFormatter(Options):
@classmethod
def info(cls) -> str:
cache = "## Base Formatter\n"
cache += BaseFormatter._info()
cache += f"## {cls.name}\n"
cache += cls._info()
infos = {BaseFormatter.name: BaseFormatter._info()}
# cache = "## Base Formatter\n"
# cache += BaseFormatter._info()
# cache += f"## {cls.name}\n"
# cache += cls._info()
cache = ''
for formatter in cls.sub_formatter:
cache += formatter.info()
infos[formatter.name] = formatter._info()
for name, info in infos.items():
cache += f"## {name}\n"
cache += info
cache += '\n'
return cache
@classmethod
@ -38,7 +44,6 @@ class BaseFormatter(Options):
info = cls.add_info('logger_name', 'logger name', 'The name of the logger')
info += '\n'
info += cls.add_info('logger_tag', 'logger tag', 'The tag of the logger')
info += '\n'
return info
def format_message(self,
@ -149,9 +154,23 @@ class LevelFormatter(BaseFormatter):
return message
class StdFormatter(BaseFormatter):
name = 'StdFormatter'
sub_formatter = [TimeFormatter(), LevelFormatter()]
@classmethod
def _info(cls) -> str:
return ''
if __name__ == '__main__':
print(TimeFormatter.info())
print(TimeFormatter().format_message(LogMessage(messages=['Hello World!'])))
print(LevelFormatter.info())
print(LevelFormatter().format_message(LogMessage(messages=['Hello World!'], level=10)))
print(StdFormatter.info())
print(StdFormatter().format_message(LogMessage(messages=['Hello World!'], level=10)))

View File

@ -7,6 +7,7 @@
import unittest
from lib_not_dr.logger.formatter import BaseFormatter, TimeFormatter, LevelFormatter
from lib_not_dr.logger.structers import LogMessage
class FormatterTest(unittest.TestCase):
@ -22,3 +23,7 @@ class FormatterTest(unittest.TestCase):
def test_level_formatter(self):
formatter = LevelFormatter()
formatter.info()
def test_std_formatter(self):
formatter = BaseFormatter()
formatter.info()