diff --git a/example/logger/formatter.py b/example/logger/formatter.py index a0238bf..580ecdd 100644 --- a/example/logger/formatter.py +++ b/example/logger/formatter.py @@ -6,7 +6,7 @@ import inspect -from lib_not_dr.logger.structers import LogMessage +from lib_not_dr.logger.structure import LogMessage from lib_not_dr.logger.formatter import (StdFormatter, TimeFormatter, LevelFormatter, diff --git a/lib_not_dr/logger/formatter/__init__.py b/lib_not_dr/logger/formatter/__init__.py index ad22a21..bf46af3 100644 --- a/lib_not_dr/logger/formatter/__init__.py +++ b/lib_not_dr/logger/formatter/__init__.py @@ -9,7 +9,7 @@ from string import Template from typing import List, Union, Optional, Dict, Tuple from lib_not_dr.types.options import Options -from lib_not_dr.logger.structers import LogMessage, FormattingMessage +from lib_not_dr.logger.structure import LogMessage, FormattingMessage class BaseFormatter(Options): @@ -202,6 +202,24 @@ class StdFormatter(BaseFormatter): TraceColorFormatter(), MessageColorFormatter()] + def __init__(self, + enable_color: bool = True, + sub_formatter: Optional[List[BaseFormatter]] = None, + color_formatters: Optional[List[BaseFormatter]] = None, + **kwargs) -> None: + """ + Initialize the StdFormatter + :param enable_color: enable color + :param sub_formatter: list of sub formatter + :param color_formatters: list of color formatter + :param kwargs: other options + """ + # 同 structures.LogMessage.__init__ 的注释 (逃) + super().__init__(enable_color=enable_color, + sub_formatter=sub_formatter, + color_formatters=color_formatters, + **kwargs) + def _format(self, message: FormattingMessage) -> FormattingMessage: super()._format(message) diff --git a/lib_not_dr/logger/formatter/colors.py b/lib_not_dr/logger/formatter/colors.py index c1878ef..3de19d4 100644 --- a/lib_not_dr/logger/formatter/colors.py +++ b/lib_not_dr/logger/formatter/colors.py @@ -7,7 +7,7 @@ from typing import Dict, Tuple from lib_not_dr.logger.formatter import BaseFormatter -from lib_not_dr.logger.structers import FormattingMessage +from lib_not_dr.logger.structure import FormattingMessage __all__ = [ diff --git a/lib_not_dr/logger/logger.py b/lib_not_dr/logger/logger.py index 255a731..f97e47b 100644 --- a/lib_not_dr/logger/logger.py +++ b/lib_not_dr/logger/logger.py @@ -9,7 +9,7 @@ import inspect from types import FrameType from typing import List, Optional -from lib_not_dr.logger.structers import LogMessage +from lib_not_dr.logger.structure import LogMessage from lib_not_dr.logger.outstream import BaseOutputStream from lib_not_dr.types.options import Options diff --git a/lib_not_dr/logger/outstream.py b/lib_not_dr/logger/outstream.py index d1921ef..81ebcc6 100644 --- a/lib_not_dr/logger/outstream.py +++ b/lib_not_dr/logger/outstream.py @@ -7,7 +7,7 @@ import sys from lib_not_dr.types.options import Options -from lib_not_dr.logger.structers import LogMessage +from lib_not_dr.logger.structure import LogMessage from lib_not_dr.logger.formatter import BaseFormatter, StdFormatter __all__ = [ @@ -21,6 +21,8 @@ class BaseOutputStream(Options): level: int = 20 enable: bool = True + formatter: BaseFormatter + def write_stdout(self, message: LogMessage) -> None: raise NotImplementedError(f'{self.__class__.__name__}.write_stdout is not implemented') @@ -60,3 +62,9 @@ class StdioOutputStream(BaseOutputStream): print('', end='', flush=True) print('', end='', flush=True, file=sys.stderr) return None + + +class FileCacheOutputStream(BaseOutputStream): + name = 'FileCacheOutputStream' + + formatter: BaseFormatter = StdFormatter(enable_color=False) \ No newline at end of file diff --git a/lib_not_dr/logger/structers.py b/lib_not_dr/logger/structers.py deleted file mode 100644 index af2f713..0000000 --- a/lib_not_dr/logger/structers.py +++ /dev/null @@ -1,60 +0,0 @@ -# ------------------------------- -# Difficult Rocket -# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com -# All rights reserved -# ------------------------------- - -import time - -from types import FrameType -from typing import List, Optional, Tuple, Dict - -from lib_not_dr.types.options import Options - -__all__ = ['LogMessage', - 'FormattingMessage'] - - -class LogMessage(Options): - name = 'LogMessage' - - # 消息内容本身的属性 - messages: List[str] = [] - end: str = '\n' - split: str = ' ' - - # 消息的属性 - flush: bool = True - level: int = 20 - log_time: float = None # time.time_ns() - logger_name: str = 'root' - logger_tag: Optional[str] = None - stack_trace: Optional[FrameType] = None - - def init(self, **kwargs) -> bool: - if self.log_time is None: - 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 - - -FormattingMessage = Tuple[LogMessage, Dict[str, str]] - -if __name__ == '__main__': - print(LogMessage().as_markdown()) diff --git a/lib_not_dr/logger/structure.py b/lib_not_dr/logger/structure.py new file mode 100644 index 0000000..c406d1f --- /dev/null +++ b/lib_not_dr/logger/structure.py @@ -0,0 +1,98 @@ +# ------------------------------- +# Difficult Rocket +# Copyright © 2020-2023 by shenjackyuanjie 3695888@qq.com +# All rights reserved +# ------------------------------- + +import time + +from types import FrameType +from typing import List, Optional, Tuple, Dict + +from lib_not_dr.types.options import Options + +__all__ = ['LogMessage', + 'FormattingMessage'] + + +class LogMessage(Options): + name = 'LogMessage' + + # 消息内容本身的属性 + messages: List[str] = [] + end: str = '\n' + split: str = ' ' + + # 消息的属性 + flush: bool = True + level: int = 20 + log_time: float = None # time.time_ns() + logger_name: str = 'root' + logger_tag: Optional[str] = None + stack_trace: Optional[FrameType] = None + + def __init__(self, + messages: Optional[List[str]] = None, + end: Optional[str] = '\n', + split: Optional[str] = ' ', + flush: Optional[bool] = True, + level: Optional[int] = 20, + log_time: Optional[float] = None, + logger_name: Optional[str] = 'root', + logger_tag: Optional[str] = None, + stack_trace: Optional[FrameType] = None, + **kwargs) -> None: + """ + Init for LogMessage + :param messages: message list for log + :param end: end of message + :param split: split for messages + :param flush: do flush or not + :param level: level of message + :param log_time: time of message (default: time.time_ns()) + :param logger_name: name of logger + :param logger_tag: tag of logger + :param stack_trace: stack trace of logger + :param kwargs: other options + """ + # 为了方便使用 单独覆盖了 __init__ 方法来提供代码补全的选项 + super().__init__(messages=messages, + end=end, + split=split, + flush=flush, + level=level, + log_time=log_time, + logger_name=logger_name, + logger_tag=logger_tag, + stack_trace=stack_trace, + **kwargs) + + def init(self, **kwargs) -> bool: + if self.log_time is None: + self.log_time = time.time_ns() + if not isinstance(self.flush, bool): + self.flush = True if self.flush else False + 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 + + +FormattingMessage = Tuple[LogMessage, Dict[str, str]] + +if __name__ == '__main__': + print(LogMessage().as_markdown()) diff --git a/test/logger/formatter.py b/test/logger/formatter.py index 64bae2c..7cdd067 100644 --- a/test/logger/formatter.py +++ b/test/logger/formatter.py @@ -8,7 +8,7 @@ import inspect import unittest from lib_not_dr.logger.formatter import BaseFormatter, TimeFormatter, LevelFormatter, TraceFormatter -from lib_not_dr.logger.structers import LogMessage +from lib_not_dr.logger.structure import LogMessage class FormatterTest(unittest.TestCase):