From 628de743eb45c19a668d402c3833f7606ff41b89 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Tue, 24 Oct 2023 00:02:24 +0800 Subject: [PATCH] sync lib-not-dr --- libs/lib_not_dr/logger/formatter.py | 29 +++++++++++++++++++++-------- libs/lib_not_dr/logger/structers.py | 13 +++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/libs/lib_not_dr/logger/formatter.py b/libs/lib_not_dr/logger/formatter.py index 4941e5f..62d36a3 100644 --- a/libs/lib_not_dr/logger/formatter.py +++ b/libs/lib_not_dr/logger/formatter.py @@ -7,7 +7,7 @@ import time from string import Template -from typing import List, Tuple, Dict, Union, Optional +from typing import List, Union, Optional from lib_not_dr.types.options import Options from structers import LogMessage, FormattingMessage @@ -17,7 +17,7 @@ class BaseFormatter(Options): name = 'BaseFormatter' sub_formatter: List['BaseFormatter'] = [] - default_template: str = '${log_time} ${logger_name} ${logger_tag} ${level} ${messages}' + default_template: str = '${log_time}|${logger_name}|${logger_tag}|${level}|${messages}' @classmethod def add_info(cls, match: str, to: str, description: str) -> str: @@ -25,7 +25,9 @@ class BaseFormatter(Options): @classmethod def info(cls) -> str: - cache = f"## {cls.name}\n" + cache = "## Base Formatter\n" + cache += BaseFormatter._info() + cache += f"## {cls.name}\n" cache += cls._info() for formatter in cls.sub_formatter: cache += formatter.info() @@ -33,7 +35,11 @@ class BaseFormatter(Options): @classmethod def _info(cls) -> str: - return '' + 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, message: LogMessage, @@ -44,9 +50,8 @@ class BaseFormatter(Options): :param template: 日志输出模板 :return: """ - basic_info = message.option() - if basic_info[''] - message, info = self._format((message, message.option())) + basic_info = message.format_for_message() + message, info = self._format((message, basic_info)) if template is None: template = Template(self.default_template) @@ -89,6 +94,14 @@ class TimeFormatter(BaseFormatter): return message +class LevelFormatter(BaseFormatter): + name = 'LevelFormatter' + + @classmethod + def _info(cls) -> str: + return cls.add_info('level', 'log level', 'The log level') + + if __name__ == '__main__': print(TimeFormatter.info()) - print(TimeFormatter().format_message(LogMessage())) + print(TimeFormatter().format_message(LogMessage(messages=['Hello World!']))) diff --git a/libs/lib_not_dr/logger/structers.py b/libs/lib_not_dr/logger/structers.py index 7d9c567..abcb4be 100644 --- a/libs/lib_not_dr/logger/structers.py +++ b/libs/lib_not_dr/logger/structers.py @@ -36,6 +36,19 @@ class LogMessage(Options): self.log_time = time.time_ns() return False + def format_message(self) -> str: + return self.split.join(self.messages) + self.end + + def format_for_message(self) -> Dict[str, str]: + basic_info = self.option() + + if self.logger_tag is None: + basic_info['logger_tag'] = ' ' + + basic_info['messages'] = self.format_message() + + return basic_info + @property def create_msec_3(self) -> int: return int(self.log_time / 1000000) % 1000