lib-not-dr/test/logger/formatter.py

47 lines
1.5 KiB
Python
Raw Normal View History

2023-10-25 00:36:57 +08:00
# -------------------------------
# Difficult Rocket
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
# All rights reserved
# -------------------------------
2023-11-01 22:23:55 +08:00
import inspect
2023-10-25 00:36:57 +08:00
import unittest
2023-12-03 21:54:28 +08:00
from lib_not_dr.loggers.formatter import BaseFormatter, MainFormatter
from lib_not_dr.loggers.structure import LogMessage
2023-10-25 00:36:57 +08:00
class FormatterTest(unittest.TestCase):
2023-11-06 00:19:39 +08:00
test_levels = (0, 5, 7, 10, 20, 30, 40, 50)
2023-11-02 22:23:53 +08:00
2023-11-01 22:23:55 +08:00
def test_create_message(self):
2023-12-03 16:36:01 +08:00
message = LogMessage(
messages=["test"],
level=20,
logger_name="root",
logger_tag="root",
stack_trace=inspect.currentframe(),
)
self.assertEqual(message.messages, ["test"])
2023-11-01 22:23:55 +08:00
self.assertEqual(message.level, 20)
2023-12-03 16:36:01 +08:00
self.assertEqual(message.logger_name, "root")
self.assertEqual(message.logger_tag, "root")
2023-11-01 22:23:55 +08:00
def test_format_level(self):
formatter = MainFormatter()
2023-10-25 00:36:57 +08:00
formatter.info()
2023-12-03 16:36:01 +08:00
message = LogMessage(messages=["test"], level=0)
2023-11-02 22:23:53 +08:00
for test_level in self.test_levels:
message.level = test_level
basic_info = message.format_for_message()
formatting_message = formatter._format((message, basic_info))
2023-12-03 16:36:01 +08:00
self.assertEqual(
formatting_message[1]["level"],
formatter.level_name_map[formatting_message[0].level],
)
2023-10-25 06:20:26 +08:00
def test_std_formatter(self):
formatter = BaseFormatter()
formatter.info()