64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
# -------------------------------
|
|
# Difficult Rocket
|
|
# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com
|
|
# All rights reserved
|
|
# -------------------------------
|
|
|
|
import time
|
|
|
|
from string import Template
|
|
from typing import List
|
|
from lib_not_dr.types.options import Options
|
|
|
|
from structers import LogMessage
|
|
|
|
|
|
class BaseFormatter(Options):
|
|
name = 'BaseFormatter'
|
|
|
|
sub_formatter: List['BaseFormatter'] = []
|
|
|
|
@classmethod
|
|
def add_info(cls, match: str, to: str, description: str) -> str:
|
|
return f'- {{{to}}} -> {match} : {description}'
|
|
|
|
@classmethod
|
|
def info(cls) -> str:
|
|
cache = f"##{cls.name}\n"
|
|
cache += cls._info()
|
|
for formatter in cls.sub_formatter:
|
|
cache += formatter.info()
|
|
return cache
|
|
|
|
@classmethod
|
|
def _info(cls) -> str:
|
|
return ''
|
|
|
|
def format(self, message: LogMessage) -> str:
|
|
raise NotImplementedError(f'{self.__class__.__name__}.format is not implemented')
|
|
|
|
def _format(self, message: LogMessage) -> LogMessage:
|
|
"""
|
|
Format message
|
|
:param message:
|
|
:return:
|
|
"""
|
|
for formatter in self.sub_formatter:
|
|
message = formatter._format(message)
|
|
return message
|
|
|
|
|
|
class TimeFormatter(BaseFormatter):
|
|
name = 'TimeFormatter'
|
|
|
|
time_format: str = '%Y-%m-%d %H:%M:%S'
|
|
msec_time_format: str = '{}-{:03d}'
|
|
|
|
@classmethod
|
|
def _info(cls) -> str:
|
|
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}]'
|