rename and add more

This commit is contained in:
shenjack 2023-12-03 21:54:28 +08:00
parent ccdb57613b
commit d272c8870d
Signed by: shenjack
GPG Key ID: 7B1134A979775551
18 changed files with 134 additions and 82 deletions

View File

@ -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")

View File

@ -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

View File

@ -0,0 +1,7 @@
[tool.lndl.nuitka.cli]
main = 'test.py'
standalone = true
clang = true
msvc = 'latest'
mingw64 = false

View File

@ -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__":

View File

@ -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!")

View File

@ -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(

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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:

View File

@ -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,

View File

@ -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"]

View File

@ -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 的初始化太慢了 所以改为不继承 直接编写

View File

@ -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):

View File

@ -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):

View File

@ -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):