some logger
This commit is contained in:
parent
fcfe300c7a
commit
1d05f9d4d7
@ -7,24 +7,25 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from string import Template
|
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 lib_not_dr.types.options import Options
|
||||||
|
|
||||||
from structers import LogMessage
|
from structers import LogMessage, FormattingMessage
|
||||||
|
|
||||||
|
|
||||||
class BaseFormatter(Options):
|
class BaseFormatter(Options):
|
||||||
name = 'BaseFormatter'
|
name = 'BaseFormatter'
|
||||||
|
|
||||||
sub_formatter: List['BaseFormatter'] = []
|
sub_formatter: List['BaseFormatter'] = []
|
||||||
|
default_template: str = '${log_time} ${logger_name} ${logger_tag} ${level} ${messages}'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def add_info(cls, match: str, to: str, description: str) -> str:
|
def add_info(cls, match: str, to: str, description: str) -> str:
|
||||||
return f'- {{{to}}} -> {match} : {description}'
|
return f'- {to} -> ${{{match}}} : {description}'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def info(cls) -> str:
|
def info(cls) -> str:
|
||||||
cache = f"##{cls.name}\n"
|
cache = f"## {cls.name}\n"
|
||||||
cache += cls._info()
|
cache += cls._info()
|
||||||
for formatter in cls.sub_formatter:
|
for formatter in cls.sub_formatter:
|
||||||
cache += formatter.info()
|
cache += formatter.info()
|
||||||
@ -34,10 +35,30 @@ class BaseFormatter(Options):
|
|||||||
def _info(cls) -> str:
|
def _info(cls) -> str:
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
def format(self, message: LogMessage) -> str:
|
def format_message(self,
|
||||||
raise NotImplementedError(f'{self.__class__.__name__}.format is not implemented')
|
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
|
Format message
|
||||||
:param message:
|
:param message:
|
||||||
@ -59,5 +80,15 @@ class TimeFormatter(BaseFormatter):
|
|||||||
return cls.add_info('log_time', 'formatted time when logging', 'The time format string'
|
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.')
|
'. See https://docs.python.org/3/library/time.html#time.strftime for more information.')
|
||||||
|
|
||||||
def format(self, message: LogMessage) -> str:
|
def _format(self, message: FormattingMessage) -> FormattingMessage:
|
||||||
return f'[{message.log_time}]'
|
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()))
|
||||||
|
@ -7,10 +7,13 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from types import FrameType
|
from types import FrameType
|
||||||
from typing import List, Optional
|
from typing import List, Optional, Tuple, Dict
|
||||||
|
|
||||||
from lib_not_dr.types.options import Options
|
from lib_not_dr.types.options import Options
|
||||||
|
|
||||||
|
__all__ = ['LogMessage',
|
||||||
|
'FormattingMessage']
|
||||||
|
|
||||||
|
|
||||||
class LogMessage(Options):
|
class LogMessage(Options):
|
||||||
name = 'LogMessage'
|
name = 'LogMessage'
|
||||||
@ -37,3 +40,5 @@ class LogMessage(Options):
|
|||||||
def create_msec_3(self) -> int:
|
def create_msec_3(self) -> int:
|
||||||
return int(self.log_time / 1000000) % 1000
|
return int(self.log_time / 1000000) % 1000
|
||||||
|
|
||||||
|
|
||||||
|
FormattingMessage = Tuple[LogMessage, Dict[str, str]]
|
||||||
|
Loading…
Reference in New Issue
Block a user