From d272c8870deda20b1ea830e5501922c1113074e4 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 3 Dec 2023 21:54:28 +0800 Subject: [PATCH] rename and add more --- README.md | 2 +- docs/change_log.md | 15 ++++++ example/lndl-nuitka/example1.toml | 7 +++ example/logger/formatter.py | 4 +- example/logger/logger.py | 6 +-- example/logger/outstream.py | 4 +- src/lib_not_dr/__init__.py | 5 +- .../{logger => loggers}/__init__.py | 50 +++++++++++++------ src/lib_not_dr/{logger => loggers}/config.py | 44 ++++++++++------ .../{logger => loggers}/formatter/__init__.py | 12 ++--- .../{logger => loggers}/formatter/colors.py | 8 +-- src/lib_not_dr/{logger => loggers}/logger.py | 39 +++++++-------- .../{logger => loggers}/outstream.py | 6 +-- .../{logger => loggers}/structer.md | 0 .../{logger => loggers}/structure.py | 6 +-- test/logger/config.py | 2 +- test/logger/formatter.py | 4 +- test/logger/structure.py | 2 +- 18 files changed, 134 insertions(+), 82 deletions(-) create mode 100644 example/lndl-nuitka/example1.toml rename src/lib_not_dr/{logger => loggers}/__init__.py (75%) rename src/lib_not_dr/{logger => loggers}/config.py (92%) rename src/lib_not_dr/{logger => loggers}/formatter/__init__.py (96%) rename src/lib_not_dr/{logger => loggers}/formatter/colors.py (97%) rename src/lib_not_dr/{logger => loggers}/logger.py (92%) rename src/lib_not_dr/{logger => loggers}/outstream.py (98%) rename src/lib_not_dr/{logger => loggers}/structer.md (100%) rename src/lib_not_dr/{logger => loggers}/structure.py (95%) diff --git a/README.md b/README.md index 3b880f1..c195e0c 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ pip install lib-not-dr[nuitka] > 等待 0.4.0 ```python title="logger.py" -from lib_not_dr.logger.logger import Logger +from lib_not_dr.loggers.logger import Logger logger = Logger.get_logger_by_name("test") diff --git a/docs/change_log.md b/docs/change_log.md index 2ac7753..36c245a 100644 --- a/docs/change_log.md +++ b/docs/change_log.md @@ -12,6 +12,21 @@ - `[tool.lndl.nuitka]` - `script = "script.py"` +### Logger + +- 将 `logger` 模块重命名为 `loggers` + +- 添加了 `get_config` 函数 + - 用于获取全局配置 + - 我也不确定有啥用捏 +- 添加了 `read_config` 函数 + - 用于向指定 `ConfigStorage`/全局 `ConfigStorage` 实例中添加配置 +- 添加了 `get_logger` 函数 + - 用于从指定 `ConfigStorage`/全局 `ConfigStorage` 实例中获取指定名称的 `Logger` 实例 +- `Logger` + - 添加了 `clone_logger` 函数 + - 用于克隆一个新的配置相同的 `Logger` 实例 + ## 0.2.3 ### lndl-nuitka diff --git a/example/lndl-nuitka/example1.toml b/example/lndl-nuitka/example1.toml new file mode 100644 index 0000000..fd5827a --- /dev/null +++ b/example/lndl-nuitka/example1.toml @@ -0,0 +1,7 @@ +[tool.lndl.nuitka.cli] +main = 'test.py' + +standalone = true +clang = true +msvc = 'latest' +mingw64 = false diff --git a/example/logger/formatter.py b/example/logger/formatter.py index 07dcf80..e54c276 100644 --- a/example/logger/formatter.py +++ b/example/logger/formatter.py @@ -6,8 +6,8 @@ import inspect -from lib_not_dr.logger.structure import LogMessage -from lib_not_dr.logger.formatter import StdFormatter +from lib_not_dr.loggers.structure import LogMessage +from lib_not_dr.loggers.formatter import StdFormatter if __name__ == "__main__": diff --git a/example/logger/logger.py b/example/logger/logger.py index f0008ff..b995f19 100644 --- a/example/logger/logger.py +++ b/example/logger/logger.py @@ -4,7 +4,7 @@ # All rights reserved # ------------------------------- -from lib_not_dr.logger.logger import Logger +from lib_not_dr import loggers import logging @@ -21,7 +21,7 @@ def logging_logger() -> None: def lndl_logger() -> None: - logger = Logger.get_logger_by_name("test") + logger = loggers.get_logger("test") logger.global_level = 20 logger.info("Hello World!") @@ -34,7 +34,7 @@ def lndl_logger() -> None: def main(): - logger = Logger.get_logger_by_name("test") + logger = loggers.get_logger("test") logger.global_level = 0 logger.info("Hello World!") diff --git a/example/logger/outstream.py b/example/logger/outstream.py index d41bcbf..7f7605f 100644 --- a/example/logger/outstream.py +++ b/example/logger/outstream.py @@ -7,8 +7,8 @@ import time import inspect -from lib_not_dr.logger.structure import LogMessage -from lib_not_dr.logger.outstream import FileCacheOutputStream, StdioOutputStream +from lib_not_dr.loggers.structure import LogMessage +from lib_not_dr.loggers.outstream import FileCacheOutputStream, StdioOutputStream if __name__ == "__main__": log_message = LogMessage( diff --git a/src/lib_not_dr/__init__.py b/src/lib_not_dr/__init__.py index e1af4e6..cf0de1e 100644 --- a/src/lib_not_dr/__init__.py +++ b/src/lib_not_dr/__init__.py @@ -7,7 +7,7 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: - from lib_not_dr import logger, nuitka, types, command + from lib_not_dr import loggers, nuitka, types, command _version_ = "0.3.0-rc.1" @@ -17,6 +17,7 @@ __all__ = [ "logger", "nuitka", "types", - "command" + "command", + "logger", ] # fmt: on diff --git a/src/lib_not_dr/logger/__init__.py b/src/lib_not_dr/loggers/__init__.py similarity index 75% rename from src/lib_not_dr/logger/__init__.py rename to src/lib_not_dr/loggers/__init__.py index d70ab67..4275826 100644 --- a/src/lib_not_dr/logger/__init__.py +++ b/src/lib_not_dr/loggers/__init__.py @@ -44,24 +44,24 @@ class LogLevel(Options): level_name_map: Dict[int, str] = { notset: notset_name, - trace: trace_name, - fine: fine_name, - debug: debug_name, - info: info_name, - warn: warn_name, - error: error_name, - fatal: fatal_name, + trace: trace_name, + fine: fine_name, + debug: debug_name, + info: info_name, + warn: warn_name, + error: error_name, + fatal: fatal_name, } name_level_map: Dict[str, int] = { notset_name: notset, - trace_name: trace, - fine_name: fine, - debug_name: debug, - info_name: info, - warn_name: warn, - error_name: error, - fatal_name: fatal, + trace_name: trace, + fine_name: fine, + debug_name: debug, + info_name: info, + warn_name: warn, + error_name: error, + fatal_name: fatal, } @classmethod @@ -94,3 +94,25 @@ class LogLevel(Options): for level in reversed(cls.level_name_map): if level_int >= level: return cls.level_name_map[level] + + +# fmt: off +from lib_not_dr.loggers.config import get_logger, get_config, read_config + +__all__ = [ + # modules + 'logger', + 'formatter', + 'outstream', + 'structure', + 'config', + # class + 'LogLevel', + # function, + 'get_logger', + 'get_config', + 'read_config', + # variable + 'COLOR_SUPPORT', +] +# fmt: on diff --git a/src/lib_not_dr/logger/config.py b/src/lib_not_dr/loggers/config.py similarity index 92% rename from src/lib_not_dr/logger/config.py rename to src/lib_not_dr/loggers/config.py index 9bf1d83..413b7fc 100644 --- a/src/lib_not_dr/logger/config.py +++ b/src/lib_not_dr/loggers/config.py @@ -6,10 +6,10 @@ from typing import List, Set, Dict, Optional, Tuple -from lib_not_dr.logger.logger import Logger -from lib_not_dr.logger.formatter import BaseFormatter -from lib_not_dr.logger.outstream import BaseOutputStream -from lib_not_dr.logger import formatter, outstream, LogLevel +from lib_not_dr.loggers.logger import Logger +from lib_not_dr.loggers.formatter import BaseFormatter +from lib_not_dr.loggers.outstream import BaseOutputStream +from lib_not_dr.loggers import formatter, outstream, LogLevel from lib_not_dr.types.options import Options, OptionNameNotDefined @@ -25,7 +25,7 @@ class ConfigStorage(Options): fail_formatters: Dict[str, dict] = {} fail_outputs: Dict[str, dict] = {} - log: Logger = Logger(logger_name="logger-storage") + log: Logger = Logger(logger_name="loggers-storage") def have_formatter(self, formatter_name: str) -> bool: return formatter_name in self.formatters @@ -266,8 +266,8 @@ class ConfigStorage(Options): def parse_logger(self, logger_config: Dict[str, dict]) -> None: """ - Parse logger config - :param logger_config: config of logger + Parse loggers config + :param logger_config: config of loggers """ env = ConfigStorage() for logger_name, config in logger_config.items(): @@ -315,15 +315,27 @@ class ConfigStorage(Options): self.parse_logger(config.get("Logger", {})) -def gen_default_storage() -> ConfigStorage: - """ - Generate default storage - :return: - """ - _storage = ConfigStorage() - _root_logger = Logger(logger_name="root") - _storage.loggers["root"] = _root_logger +_storage = ConfigStorage(loggers={'root': Logger(logger_name='root')}) + + +def get_config() -> ConfigStorage: return _storage -storage = gen_default_storage() +def get_logger(name: str = 'root', storage: Optional[ConfigStorage] = None) -> Logger: + if storage is None: + storage = _storage + + if name not in storage.loggers: + root_log = storage.loggers['root'].clone_logger() + root_log.logger_name = name + storage.loggers[name] = root_log + return storage.loggers[name] + + +def read_config(log_config: Dict, storage: Optional[ConfigStorage] = None) -> ConfigStorage: + if storage is None: + storage = _storage + + storage.read_dict_config(log_config) + return storage diff --git a/src/lib_not_dr/logger/formatter/__init__.py b/src/lib_not_dr/loggers/formatter/__init__.py similarity index 96% rename from src/lib_not_dr/logger/formatter/__init__.py rename to src/lib_not_dr/loggers/formatter/__init__.py index 7cf7d0c..e60d87f 100644 --- a/src/lib_not_dr/logger/formatter/__init__.py +++ b/src/lib_not_dr/loggers/formatter/__init__.py @@ -9,12 +9,12 @@ from pathlib import Path from string import Template from typing import List, Union, Optional, TYPE_CHECKING -from lib_not_dr.logger import LogLevel +from lib_not_dr.loggers import LogLevel from lib_not_dr.types.options import Options -from lib_not_dr.logger.structure import LogMessage, FormattingMessage +from lib_not_dr.loggers.structure import LogMessage, FormattingMessage if TYPE_CHECKING: - from lib_not_dr.logger.formatter.colors import BaseColorFormatter + from lib_not_dr.loggers.formatter.colors import BaseColorFormatter __all__ = ["BaseFormatter", "MainFormatter", "StdFormatter"] @@ -48,11 +48,11 @@ class BaseFormatter(Options): @classmethod def _info(cls) -> str: info = cls.add_info( - "logger_name", "logger name", "The name of the logger" + "logger_name", "loggers name", "The name of the loggers" ) info += "\n" info += cls.add_info( - "logger_tag", "logger tag", "The tag of the logger" + "logger_tag", "loggers tag", "The tag of the loggers" ) return info @@ -215,7 +215,7 @@ class StdFormatter(BaseFormatter): enable_color: bool = True sub_formatter: List[BaseFormatter] = [MainFormatter()] - from lib_not_dr.logger.formatter.colors import ( + from lib_not_dr.loggers.formatter.colors import ( LevelColorFormatter, LoggerColorFormatter, TimeColorFormatter, diff --git a/src/lib_not_dr/logger/formatter/colors.py b/src/lib_not_dr/loggers/formatter/colors.py similarity index 97% rename from src/lib_not_dr/logger/formatter/colors.py rename to src/lib_not_dr/loggers/formatter/colors.py index da8204b..3342b68 100644 --- a/src/lib_not_dr/logger/formatter/colors.py +++ b/src/lib_not_dr/loggers/formatter/colors.py @@ -4,9 +4,9 @@ # All rights reserved # ------------------------------- -from lib_not_dr.logger import LogLevel, COLOR_SUPPORT -from lib_not_dr.logger.formatter import BaseFormatter -from lib_not_dr.logger.structure import FormattingMessage +from lib_not_dr.loggers import LogLevel, COLOR_SUPPORT +from lib_not_dr.loggers.formatter import BaseFormatter +from lib_not_dr.loggers.structure import FormattingMessage __all__ = [ "BaseColorFormatter", @@ -116,7 +116,7 @@ class LoggerColorFormatter(BaseColorFormatter): @classmethod def _info(cls) -> str: return cls.add_info( - "colored logger name", "logger name", "A colored logger name" + "colored loggers name", "loggers name", "A colored loggers name" ) def _format(self, message: FormattingMessage) -> FormattingMessage: diff --git a/src/lib_not_dr/logger/logger.py b/src/lib_not_dr/loggers/logger.py similarity index 92% rename from src/lib_not_dr/logger/logger.py rename to src/lib_not_dr/loggers/logger.py index f3325a9..539e7b1 100644 --- a/src/lib_not_dr/logger/logger.py +++ b/src/lib_not_dr/loggers/logger.py @@ -9,10 +9,10 @@ import inspect from types import FrameType from typing import List, Optional, Union -from lib_not_dr.logger import LogLevel +from lib_not_dr.loggers import LogLevel from lib_not_dr.types.options import Options -from lib_not_dr.logger.structure import LogMessage -from lib_not_dr.logger.outstream import BaseOutputStream, StdioOutputStream +from lib_not_dr.loggers.structure import LogMessage +from lib_not_dr.loggers.outstream import BaseOutputStream, StdioOutputStream class Logger(Options): @@ -25,6 +25,20 @@ class Logger(Options): enable: bool = True level: int = 20 # info + def clone_logger(self) -> "Logger": + """ + Clone a new loggers with the same config. + + Returns: + Logger: The cloned loggers. + """ + return Logger( + logger_name=self.logger_name, + enable=self.enable, + level=self.level, + outputs=self.outputs.copy(), + ) + def log_for(self, level: int) -> bool: """ Check if logging is enabled for a specific level. @@ -138,25 +152,6 @@ class Logger(Options): # done? # 20231106 00:06 - @staticmethod - def get_logger_by_name(name: str) -> "Logger": - """ - Get a logger by name. - - Args: - name (str): The name of the logger. - - Returns: - Logger: The logger with the specified name. - """ - from lib_not_dr.logger.config import storage - - if storage.have_logger(name): - return storage.loggers[name] - _logger = Logger(logger_name=name) - storage.loggers[name] = _logger - return _logger - def info( self, *message, diff --git a/src/lib_not_dr/logger/outstream.py b/src/lib_not_dr/loggers/outstream.py similarity index 98% rename from src/lib_not_dr/logger/outstream.py rename to src/lib_not_dr/loggers/outstream.py index e87d5d9..48cb7d1 100644 --- a/src/lib_not_dr/logger/outstream.py +++ b/src/lib_not_dr/loggers/outstream.py @@ -14,10 +14,10 @@ import threading from pathlib import Path from typing import Optional -from lib_not_dr.logger import LogLevel +from lib_not_dr.loggers import LogLevel from lib_not_dr.types.options import Options -from lib_not_dr.logger.structure import LogMessage -from lib_not_dr.logger.formatter import BaseFormatter, StdFormatter +from lib_not_dr.loggers.structure import LogMessage +from lib_not_dr.loggers.formatter import BaseFormatter, StdFormatter __all__ = ["BaseOutputStream", "StdioOutputStream", "FileCacheOutputStream"] diff --git a/src/lib_not_dr/logger/structer.md b/src/lib_not_dr/loggers/structer.md similarity index 100% rename from src/lib_not_dr/logger/structer.md rename to src/lib_not_dr/loggers/structer.md diff --git a/src/lib_not_dr/logger/structure.py b/src/lib_not_dr/loggers/structure.py similarity index 95% rename from src/lib_not_dr/logger/structure.py rename to src/lib_not_dr/loggers/structure.py index 43deaaa..fe748c2 100644 --- a/src/lib_not_dr/logger/structure.py +++ b/src/lib_not_dr/loggers/structure.py @@ -49,9 +49,9 @@ class LogMessage: :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 logger_name: name of loggers + :param logger_tag: tag of loggers + :param stack_trace: stack trace of loggers """ # 20231128 23:23 # 因为 Options 的初始化太慢了 所以改为不继承 直接编写 diff --git a/test/logger/config.py b/test/logger/config.py index f34b138..f9c4b0b 100644 --- a/test/logger/config.py +++ b/test/logger/config.py @@ -6,7 +6,7 @@ import unittest -from lib_not_dr.logger.config import ConfigStorage +from lib_not_dr.loggers.config import ConfigStorage class TestConfigStorage(unittest.TestCase): diff --git a/test/logger/formatter.py b/test/logger/formatter.py index 96af4bd..6bed5bd 100644 --- a/test/logger/formatter.py +++ b/test/logger/formatter.py @@ -7,8 +7,8 @@ import inspect import unittest -from lib_not_dr.logger.formatter import BaseFormatter, MainFormatter -from lib_not_dr.logger.structure import LogMessage +from lib_not_dr.loggers.formatter import BaseFormatter, MainFormatter +from lib_not_dr.loggers.structure import LogMessage class FormatterTest(unittest.TestCase): diff --git a/test/logger/structure.py b/test/logger/structure.py index a7b3eed..1e44f6f 100644 --- a/test/logger/structure.py +++ b/test/logger/structure.py @@ -7,7 +7,7 @@ import time import unittest -from lib_not_dr.logger.structure import LogMessage +from lib_not_dr.loggers.structure import LogMessage class LogMessageTest(unittest.TestCase):