From 8a6101087778f0048b6f4ad994bdb615cb41d2c9 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 29 Nov 2023 00:02:42 +0800 Subject: [PATCH 1/5] =?UTF-8?q?let=20LogMessage=20=E4=B8=8D=E7=BB=A7?= =?UTF-8?q?=E6=89=BF=20Options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib_not_dr/logger/structure.py | 44 +++++++++++++----------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/lib_not_dr/logger/structure.py b/src/lib_not_dr/logger/structure.py index e166403..ca5bdd8 100644 --- a/src/lib_not_dr/logger/structure.py +++ b/src/lib_not_dr/logger/structure.py @@ -16,9 +16,7 @@ __all__ = ['LogMessage', 'FormattingMessage'] -class LogMessage(Options): - name = 'LogMessage' - +class LogMessage: # 消息内容本身的属性 messages: List[str] = [] end: str = '\n' @@ -56,26 +54,25 @@ class LogMessage(Options): :param stack_trace: stack trace of logger :param kwargs: other options """ - # 为了方便使用 单独覆盖了 __init__ 方法来提供代码补全的选项 - if messages is None: - messages = [] - 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) + # 20231128 23:23 + # 因为 Options 的初始化太慢了 所以改为不继承 直接编写 + + self.messages = messages if messages is not None else [] + self.end = end + self.split = split + self.level = level + self.logger_name = logger_name + self.logger_tag = logger_tag + + + if log_time is None: + log_time = time.time_ns() + self.log_time = log_time + + if not isinstance(flush, bool) and flush is not None: + flush = True if flush else False + self.flush = flush - def init(self, **kwargs) -> bool: - if self.log_time is None: - self.log_time = time.time_ns() - if not isinstance(self.flush, bool) and self.flush is not None: - self.flush = True if self.flush else False - return False def format_message(self) -> str: if self.split is None: @@ -98,6 +95,3 @@ class LogMessage(Options): FormattingMessage = Tuple[LogMessage, Dict[str, Union[str, Path]]] - -if __name__ == '__main__': - print(LogMessage().as_markdown()) From 976da68d24736ec200ca62c051ed03dad47ebeb3 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 29 Nov 2023 00:16:04 +0800 Subject: [PATCH 2/5] =?UTF-8?q?Fix=20|=20=E6=B2=A1=E5=8A=A0=E8=BF=9B?= =?UTF-8?q?=E5=8E=BB=E7=9A=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib_not_dr/logger/structure.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/lib_not_dr/logger/structure.py b/src/lib_not_dr/logger/structure.py index ca5bdd8..1f8647e 100644 --- a/src/lib_not_dr/logger/structure.py +++ b/src/lib_not_dr/logger/structure.py @@ -39,8 +39,7 @@ class LogMessage: log_time: Optional[float] = None, logger_name: Optional[str] = 'root', logger_tag: Optional[str] = None, - stack_trace: Optional[FrameType] = None, - **kwargs) -> None: + stack_trace: Optional[FrameType] = None) -> None: """ Init for LogMessage :param messages: message list for log @@ -52,7 +51,6 @@ class LogMessage: :param logger_name: name of logger :param logger_tag: tag of logger :param stack_trace: stack trace of logger - :param kwargs: other options """ # 20231128 23:23 # 因为 Options 的初始化太慢了 所以改为不继承 直接编写 @@ -63,7 +61,7 @@ class LogMessage: self.level = level self.logger_name = logger_name self.logger_tag = logger_tag - + self.stack_trace = stack_trace if log_time is None: log_time = time.time_ns() @@ -73,6 +71,18 @@ class LogMessage: flush = True if flush else False self.flush = flush + def option(self) -> Dict[str, Union[str, int, float, bool]]: + return { + 'messages': self.messages, + 'end': self.end, + 'split': self.split, + 'flush': self.flush, + 'level': self.level, + 'log_time': self.log_time, + 'logger_name': self.logger_name, + 'logger_tag': self.logger_tag, + 'stack_trace': self.stack_trace, + } def format_message(self) -> str: if self.split is None: From 86739e3a0fd93f708a1064ce926f0c94c5518553 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 29 Nov 2023 00:31:52 +0800 Subject: [PATCH 3/5] profile --- example/logger/logger.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/example/logger/logger.py b/example/logger/logger.py index 8da2ce9..39504f7 100644 --- a/example/logger/logger.py +++ b/example/logger/logger.py @@ -11,7 +11,7 @@ import logging def logging_logger() -> None: logger = logging.getLogger('test') - logger.setLevel(logging.DEBUG) + logger.level = logging.INFO logger.info('Hello World!') logger.debug('Hello World!') @@ -22,7 +22,7 @@ def logging_logger() -> None: def lndl_logger() -> None: logger = Logger.get_logger_by_name('test') - logger.global_level = 0 + logger.global_level = 20 logger.info('Hello World!') logger.fine('Hello World!') @@ -53,5 +53,6 @@ def main(): if __name__ == '__main__': + main() lndl_logger() logging_logger() From 6e03ca7120bd6c12ef485751662a22bbea4a3a58 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Wed, 29 Nov 2023 19:31:17 +0800 Subject: [PATCH 4/5] remove the list(message) to improve speed --- src/lib_not_dr/logger/logger.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/lib_not_dr/logger/logger.py b/src/lib_not_dr/logger/logger.py index bf8d8aa..2176013 100644 --- a/src/lib_not_dr/logger/logger.py +++ b/src/lib_not_dr/logger/logger.py @@ -7,7 +7,7 @@ import time import inspect from types import FrameType -from typing import List, Optional +from typing import List, Optional, Any from lib_not_dr.logger import LogLevel from lib_not_dr.types.options import Options @@ -89,7 +89,7 @@ class Logger(Options): output.level = level def make_log(self, - messages: List[str], + messages: List[Any], tag: Optional[str] = None, end: str = '\n', split: str = ' ', @@ -159,7 +159,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.info): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, @@ -176,7 +176,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.trace): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, @@ -193,7 +193,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.fine): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, @@ -210,7 +210,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.debug): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, @@ -227,7 +227,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.warn): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, @@ -244,7 +244,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.error): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, @@ -261,7 +261,7 @@ class Logger(Options): stack_trace: Optional[FrameType] = None) -> None: if not self.log_for(LogLevel.fatal): return - self.make_log(messages=list(message), + self.make_log(messages=message, tag=tag, end=end, split=split, From 7f904ada86ccd45bfca8bec9873181c5454d05d4 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 2 Dec 2023 10:16:16 +0800 Subject: [PATCH 5/5] Enhance: finish envstorage.parse_loggger --- pyproject.toml | 1 - src/lib_not_dr/logger/__init__.py | 3 +- src/lib_not_dr/logger/config.py | 62 +++++++++++++++++++++++++++---- src/lib_not_dr/logger/logger.py | 8 ++-- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7beef4b..4f828ed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,4 @@ [project] - name = "lib-not-dr" description = "A python lib created from Difficult Rocket development" readme = "README.md" diff --git a/src/lib_not_dr/logger/__init__.py b/src/lib_not_dr/logger/__init__.py index 1b68b3a..d0c0a2e 100644 --- a/src/lib_not_dr/logger/__init__.py +++ b/src/lib_not_dr/logger/__init__.py @@ -65,8 +65,7 @@ class LogLevel(Options): } @classmethod - def parse_name_level(cls, - name: str) -> int: + def parse_name_level(cls, name: str) -> int: """ parse logging name to level int :param name: logging level name diff --git a/src/lib_not_dr/logger/config.py b/src/lib_not_dr/logger/config.py index 4471886..35962da 100644 --- a/src/lib_not_dr/logger/config.py +++ b/src/lib_not_dr/logger/config.py @@ -4,13 +4,13 @@ # All rights reserved # ------------------------------- -from typing import List, Set, Dict, Optional +from typing import List, Set, Dict, Optional, Tuple from lib_not_dr.logger.logger import Logger -from lib_not_dr.types.options import Options, OptionNameNotDefined from lib_not_dr.logger.formatter import BaseFormatter from lib_not_dr.logger.outstream import BaseOutputStream -from lib_not_dr.logger import formatter, outstream +from lib_not_dr.logger import formatter, outstream, LogLevel +from lib_not_dr.types.options import Options, OptionNameNotDefined class ConfigStorage(Options): @@ -73,10 +73,26 @@ class ConfigStorage(Options): cycles_set.update(cycle) return sorted(cycles_set) # 返回排序后的循环列表 - def parse_level(self, level_config: Dict[str, str]) -> int: + def parse_level(self, level_config: Dict[str, str]) -> Optional[int]: """ """ - + level_found: Tuple[Optional[int], Optional[str]] = (level_config.get('level'), level_config.get('level_name')) + if all(_l is None for _l in level_found): + # 如果都没有 + self.log.warn(f'No level or level_name in config {level_config}, ignored') + return 20 + if all(_l is not None for _l in level_found): + # 如果都有 那么使用 level_name + self.log.warn(f'Level and level_name both exist in config {level_config}, using level_name') + # 去掉 level 保留 level_name + level_found = (None, level_found[1],) + if level_found[0] is not None: + # 如果 level 存在 那么使用 level + return level_found[0] + if level_found[1] is not None: + # 如果 level_name 存在 那么使用 level_name + return LogLevel.parse_name_level(level_found[1]) + return None def get_class_by_name(self, config: Dict[str, str], module) -> Optional[type]: """ @@ -94,6 +110,7 @@ class ConfigStorage(Options): if class_name not in module.__all__: self.log.warn(f'Class {class_name} not found in module {module}, ignored') return None + return getattr(module, class_name) def parse_formatter(self, formatter_config: Dict[str, dict]) -> None: @@ -191,6 +208,10 @@ class ConfigStorage(Options): continue else: config['formatter'] = self.formatters[config['formatter']] + if level := self.parse_level(config) is not None: + config['level'] = level + if 'level_name' in config: + config.pop('level_name') # init output try: output_instance = output_class(**config) @@ -211,9 +232,34 @@ class ConfigStorage(Options): """ env = ConfigStorage() for logger_name, config in logger_config.items(): - ... - - + # get output for logger + if 'output' in config: + if self.outputs.get(config['output']) is None: + if self.fail_outputs.get(config['output']) is None: + self.log.error(f'Logger {logger_name} output {config["output"]} not found, ignored') + else: + self.log.error(f'Logger {logger_name} require a fail output {config["output"]}, ignored') + env.fail_loggers[logger_name] = config + continue + else: + config['output'] = self.outputs[config['output']] + if level := self.parse_level(config) is not None: + config['level'] = level + if 'level_name' in config: + config.pop('level_name') + # init logger + try: + logger_instance = Logger(**config) + except OptionNameNotDefined as e: + self.log.error(f'Logger {logger_name} init failed, ignored\n' + f'Error: {e}') + env.fail_loggers[logger_name] = config + continue + # add logger + env.loggers[logger_name] = logger_instance + self.merge_storage(env) + return None + def read_dict_config(self, config: Dict[str, dict]) -> None: """ Read config from dict diff --git a/src/lib_not_dr/logger/logger.py b/src/lib_not_dr/logger/logger.py index 2176013..8ec163f 100644 --- a/src/lib_not_dr/logger/logger.py +++ b/src/lib_not_dr/logger/logger.py @@ -7,7 +7,7 @@ import time import inspect from types import FrameType -from typing import List, Optional, Any +from typing import List, Optional, Union from lib_not_dr.logger import LogLevel from lib_not_dr.types.options import Options @@ -89,7 +89,7 @@ class Logger(Options): output.level = level def make_log(self, - messages: List[Any], + messages: Union[list, tuple], tag: Optional[str] = None, end: str = '\n', split: str = ' ', @@ -148,7 +148,9 @@ class Logger(Options): from lib_not_dr.logger.config import storage if storage.have_logger(name): return storage.loggers[name] - return Logger(logger_name=name) + _logger = Logger(logger_name=name) + storage.loggers[name] = _logger + return _logger def info(self, *message,