diff --git a/example/logger/formatter.py b/example/logger/formatter.py index 83b9d96..6b9c7f1 100644 --- a/example/logger/formatter.py +++ b/example/logger/formatter.py @@ -7,10 +7,7 @@ import inspect from lib_not_dr.logger.structure import LogMessage -from lib_not_dr.logger.formatter import (StdFormatter, - TimeFormatter, - LevelFormatter, - TraceFormatter) +from lib_not_dr.logger.formatter import (StdFormatter) if __name__ == '__main__': diff --git a/lib_not_dr/logger/structure.py b/lib_not_dr/logger/structure.py index b8ee936..0528102 100644 --- a/lib_not_dr/logger/structure.py +++ b/lib_not_dr/logger/structure.py @@ -57,6 +57,8 @@ class LogMessage(Options): :param kwargs: other options """ # 为了方便使用 单独覆盖了 __init__ 方法来提供代码补全的选项 + if messages is None: + messages = [] super().__init__(messages=messages, end=end, split=split, @@ -76,6 +78,8 @@ class LogMessage(Options): return False def format_message(self) -> str: + if self.split is None: + self.split = ' ' return self.split.join(self.messages) + self.end def format_for_message(self) -> Dict[str, str]: diff --git a/test/logger/formatter.py b/test/logger/formatter.py index 1bd6763..aa26af3 100644 --- a/test/logger/formatter.py +++ b/test/logger/formatter.py @@ -7,7 +7,7 @@ import inspect import unittest -from lib_not_dr.logger.formatter import BaseFormatter, TimeFormatter, LevelFormatter, TraceFormatter +from lib_not_dr.logger.formatter import BaseFormatter, MainFormatter from lib_not_dr.logger.structure import LogMessage @@ -26,22 +26,8 @@ class FormatterTest(unittest.TestCase): self.assertEqual(message.logger_name, 'root') self.assertEqual(message.logger_tag, 'root') - def test_create_empty_tag_message(self): - message = LogMessage() - - self.assertIsNone(message.logger_tag) - self.assertEqual(message.format_for_message()['logger_tag'], ' ') - - def test_formatter(self): - formatter = BaseFormatter() - formatter.info() - - def test_time_formatter(self): - formatter = TimeFormatter() - formatter.info() - - def test_level_formatter(self): - formatter = LevelFormatter() + def test_format_level(self): + formatter = MainFormatter() formatter.info() message = LogMessage(messages=['test'], level=0) @@ -55,7 +41,3 @@ class FormatterTest(unittest.TestCase): def test_std_formatter(self): formatter = BaseFormatter() formatter.info() - - def test_trace_formatter(self): - formatter = TraceFormatter() - formatter.info() diff --git a/test/logger/structure.py b/test/logger/structure.py new file mode 100644 index 0000000..3c4bb70 --- /dev/null +++ b/test/logger/structure.py @@ -0,0 +1,52 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +import time +import unittest + +from lib_not_dr.logger.structure import LogMessage + + +class LogMessageTest(unittest.TestCase): + def test_create_empty_message(self): + message = LogMessage() + self.assertEqual(message.messages, []) + self.assertEqual(message.level, 20) + self.assertEqual(message.logger_name, 'root') + self.assertEqual(message.logger_tag, None) + + def test_format_message_empty(self): + """ + 测试空消息的格式化 + 因为才发现空信息之前的格式化是有问题的(因为没有初始化 messages) + """ + message = LogMessage() + self.assertEqual(message.format_message(), '\n') + + def test_format_message_end(self): + """ + 测试消息的结尾 + """ + message = LogMessage(messages=['test'], + end='\n') + for testing in ('\n', '\r\n', '\r'): + message.end = testing + self.assertEqual(message.format_message(), 'test' + testing) + + def test_format_message_split(self): + """ + 测试消息的分割 + """ + message = LogMessage(messages=['test', 'test2'], end='') + for testing in (' ', ' | ', ' | '): + message.split = testing + self.assertEqual(message.format_message(), 'test' + testing + 'test2') + + def test_msec_3(self): + start_time = time.time() + message = LogMessage(log_time=start_time) + msec_3 = int(start_time / 1000000) % 1000 + self.assertEqual(message.create_msec_3, msec_3)