some logger

This commit is contained in:
shenjack 2023-10-23 23:41:22 +08:00
parent fcfe300c7a
commit 1d05f9d4d7
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 46 additions and 10 deletions

View File

@ -7,24 +7,25 @@
import time
from string import Template
from typing import List
from typing import List, Tuple, Dict, Union, Optional
from lib_not_dr.types.options import Options
from structers import LogMessage
from structers import LogMessage, FormattingMessage
class BaseFormatter(Options):
name = 'BaseFormatter'
sub_formatter: List['BaseFormatter'] = []
default_template: str = '${log_time} ${logger_name} ${logger_tag} ${level} ${messages}'
@classmethod
def add_info(cls, match: str, to: str, description: str) -> str:
return f'- {{{to}}} -> {match} : {description}'
return f'- {to} -> ${{{match}}} : {description}'
@classmethod
def info(cls) -> str:
cache = f"##{cls.name}\n"
cache = f"## {cls.name}\n"
cache += cls._info()
for formatter in cls.sub_formatter:
cache += formatter.info()
@ -34,10 +35,30 @@ class BaseFormatter(Options):
def _info(cls) -> str:
return ''
def format(self, message: LogMessage) -> str:
raise NotImplementedError(f'{self.__class__.__name__}.format is not implemented')
def format_message(self,
message: LogMessage,
template: Optional[Union[Template, str]] = None) -> str:
"""
Format message
:param message: 输入的消息
:param template: 日志输出模板
:return:
"""
basic_info = message.option()
if basic_info['']
message, info = self._format((message, message.option()))
def _format(self, message: LogMessage) -> LogMessage:
if template is None:
template = Template(self.default_template)
elif isinstance(template, str):
template = Template(template)
try:
return template.substitute(**info)
except KeyError:
return template.safe_substitute(**info)
def _format(self, message: FormattingMessage) -> FormattingMessage:
"""
Format message
:param message:
@ -59,5 +80,15 @@ class TimeFormatter(BaseFormatter):
return cls.add_info('log_time', 'formatted time when logging', 'The time format string'
'. See https://docs.python.org/3/library/time.html#time.strftime for more information.')
def format(self, message: LogMessage) -> str:
return f'[{message.log_time}]'
def _format(self, message: FormattingMessage) -> FormattingMessage:
time_mark = time.localtime(message[0].log_time / 1000000000)
if self.msec_time_format:
time_mark = self.msec_time_format.format(time.strftime(self.time_format, time_mark),
message[0].create_msec_3)
message[1]['log_time'] = time_mark
return message
if __name__ == '__main__':
print(TimeFormatter.info())
print(TimeFormatter().format_message(LogMessage()))

View File

@ -7,10 +7,13 @@
import time
from types import FrameType
from typing import List, Optional
from typing import List, Optional, Tuple, Dict
from lib_not_dr.types.options import Options
__all__ = ['LogMessage',
'FormattingMessage']
class LogMessage(Options):
name = 'LogMessage'
@ -37,3 +40,5 @@ class LogMessage(Options):
def create_msec_3(self) -> int:
return int(self.log_time / 1000000) % 1000
FormattingMessage = Tuple[LogMessage, Dict[str, str]]