mlog-util 0.1.3__py3-none-any.whl → 0.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mlog-util might be problematic. Click here for more details.

mlog_util/log_manager.py CHANGED
@@ -6,87 +6,80 @@ from typing import Type, List, Optional
6
6
  # from .handlers import MultiProcessSafeSizeRotatingHandler, MultiProcessSafeTimeRotatingHandler
7
7
 
8
8
  class LogManager:
9
- _logger_cache = {}
10
- _lock = threading.Lock() # 多线程安全
9
+ """
10
+ 一个线程安全的日志管理器,用于获取和配置具有 Rich 控制台输出的 Logger。
11
+ """
12
+ _logger_cache: dict[str, logging.Logger] = {}
13
+ _lock = threading.Lock()
11
14
 
12
15
  @classmethod
13
16
  def get_logger(
14
17
  cls,
15
18
  name: str,
16
- logger_cls: Type[logging.Logger] = logging.Logger,
17
- log_file: str | None = None,
19
+ log_file: Optional[str] = None,
18
20
  add_console: bool = True,
19
21
  level: int = logging.INFO,
20
- custom_handlers: list[logging.Handler] | None = None,
22
+ custom_handlers: Optional[List[logging.Handler]] = None,
21
23
  ) -> logging.Logger:
22
24
  """
23
- 获取或创建 logger。
25
+ 获取或创建一个配置好的 logger。
26
+
27
+ 注意:Logger 实例按 name 缓存。重复调用会返回同一个实例,
28
+ 但会确保其配置(如 level 和 handlers)符合当前调用参数。
24
29
 
25
- :param name: logger 名称
26
- :param logger_cls: logger
27
- :param log_file: 日志文件路径
28
- :param add_console: 是否添加控制台 RichHandler
29
- :param level: 日志级别
30
- :param custom_handlers: 自定义 Handler 列表
30
+ :param name: logger 名称。
31
+ :param log_file: 日志文件路径,如果为 None 则不写入文件。
32
+ :param add_console: 是否添加带 Rich 格式的控制台 Handler。
33
+ :param level: 日志级别。
34
+ :param custom_handlers: 自定义 Handler 列表。
31
35
  """
32
- cache_key = (name, logger_cls)
33
36
  with cls._lock:
34
- if cache_key not in cls._logger_cache:
35
- # 创建 logger
36
- if logger_cls == logging.Logger:
37
- logger = logging.getLogger(name)
38
- else:
39
- logger = logger_cls(name)
40
-
41
- logger.setLevel(level)
42
- logger.propagate = False # 不向 root logger 冒泡
43
-
44
- # 添加控制台 handler
45
- if add_console:
46
- if not any(isinstance(h, RichHandler) for h in logger.handlers):
47
- console_handler = RichHandler(rich_tracebacks=True)
48
- console_formatter = logging.Formatter(
49
- "%(message)s [%(name)s - %(asctime)s]",
50
- datefmt="%Y-%m-%d %H:%M:%S"
51
- )
52
- console_handler.setFormatter(console_formatter)
53
- logger.addHandler(console_handler)
54
-
55
- # 添加文件 handler
56
- if log_file:
57
- if not any(isinstance(h, logging.FileHandler) for h in logger.handlers):
58
- # handler = MultiProcessSafeSizeRotatingHandler(log_file, maxBytes=10*200, backupCount=3)
59
- # file_handler = logging.FileHandler(log_file, encoding="utf-8")
60
-
61
- handler = logging.FileHandler(log_file, encoding="utf-8")
62
- file_formatter = logging.Formatter(
63
- "%(asctime)s | %(name)s | %(levelname)s | %(message)s",
64
- datefmt="%Y-%m-%d %H:%M:%S"
65
- )
66
- handler.setFormatter(file_formatter)
37
+ # 1. 获取或创建 Logger 实例 (利用 logging 模块自身的缓存)
38
+ logger = logging.getLogger(name)
39
+
40
+ # 2. 确保基本配置
41
+ logger.setLevel(level)
42
+ logger.propagate = False
43
+
44
+ # 3. 配置控制台 Handler
45
+ if add_console and not any(isinstance(h, RichHandler) for h in logger.handlers):
46
+ console_handler = RichHandler(rich_tracebacks=True, show_time=False, show_path=False)
47
+ # 注意:RichHandler 默认有自己的时间格式,我们可以在 Formatter 中覆盖
48
+ console_formatter = logging.Formatter(
49
+ "[%(name)s - %(asctime)s] %(message)s",
50
+ datefmt="%Y-%m-%d %H:%M:%S"
51
+ )
52
+ console_handler.setFormatter(console_formatter)
53
+ logger.addHandler(console_handler)
54
+
55
+ # 4. 配置文件 Handler
56
+ if log_file and not any(isinstance(h, logging.FileHandler) for h in logger.handlers):
57
+ file_handler = logging.FileHandler(log_file, encoding="utf-8")
58
+ file_formatter = logging.Formatter(
59
+ "%(asctime)s | %(name)s | %(levelname)-8s | %(message)s",
60
+ datefmt="%Y-%m-%d %H:%M:%S"
61
+ )
62
+ file_handler.setFormatter(file_formatter)
63
+ logger.addHandler(file_handler)
64
+
65
+ # 5. 配置自定义 Handlers (修正了原代码的 Bug)
66
+ if custom_handlers:
67
+ # 为所有自定义 handlers 设置一个统一的格式
68
+ custom_formatter = logging.Formatter(
69
+ "%(asctime)s | %(name)s | %(levelname)-8s | %(message)s",
70
+ datefmt="%Y-%m-%d %H:%M:%S"
71
+ )
72
+ for handler in custom_handlers:
73
+ if handler not in logger.handlers:
74
+ handler.setFormatter(custom_formatter)
67
75
  logger.addHandler(handler)
68
76
 
69
- # 添加自定义 handler
70
- if custom_handlers:
71
- h = custom_handlers
72
- if h not in logger.handlers:
73
- file_formatter = logging.Formatter(
74
- "%(asctime)s | %(name)s | %(levelname)s | %(message)s",
75
- datefmt="%Y-%m-%d %H:%M:%S"
76
- )
77
- h.setFormatter(file_formatter)
78
- logger.addHandler(h)
79
- logger.addHandler(h)
80
-
81
- cls._logger_cache[cache_key] = logger
82
-
83
- return cls._logger_cache[cache_key]
77
+ return logger
84
78
 
85
79
 
86
80
  # 全局可用的 get_logger 函数(无需引用 LogManager)
87
81
  def get_logger(
88
82
  name: str=None,
89
- logger_cls: Type[logging.Logger] = logging.Logger,
90
83
  log_file: Optional[str] = None,
91
84
  add_console: bool = True,
92
85
  level: int = logging.INFO,
@@ -105,7 +98,6 @@ def get_logger(
105
98
 
106
99
  return LogManager.get_logger(
107
100
  name=name,
108
- logger_cls=logger_cls,
109
101
  log_file=log_file,
110
102
  add_console=add_console,
111
103
  level=level,
@@ -0,0 +1,8 @@
1
+ Metadata-Version: 2.4
2
+ Name: mlog-util
3
+ Version: 0.1.6
4
+ Summary: Add your description here
5
+ Requires-Python: >=3.12
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: rich>=14.2.0
8
+ Requires-Dist: portalocker
@@ -0,0 +1,7 @@
1
+ mlog_util/__init__.py,sha256=ZzcwRSCJDIHq_enQIRpvl8VRZz7izkL4VsnupRsR3AY,144
2
+ mlog_util/handlers.py,sha256=0EXqAlzxVlcfXEO478EdYJSDdmlZZp5lRame4hEGnmA,10638
3
+ mlog_util/log_manager.py,sha256=xD1_RmN3O9u1nxqhMqKxAhY00x0pBj_jpvxgL4cFZBE,4088
4
+ mlog_util-0.1.6.dist-info/METADATA,sha256=QLY0lX7AHZCfz7VhKFeK9UdurOFg5p5e9mH-ZZvvVTM,207
5
+ mlog_util-0.1.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
+ mlog_util-0.1.6.dist-info/top_level.txt,sha256=-liqOVoloTs4GLglhy_pzBBpK-ltsV3IZpg0OQsoD_4,10
7
+ mlog_util-0.1.6.dist-info/RECORD,,
@@ -1,14 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mlog_util
3
- Version: 0.1.3
4
- Summary: 自用日志库
5
- Classifier: Programming Language :: Python :: 3
6
- Classifier: License :: OSI Approved :: MIT License
7
- Classifier: Operating System :: OS Independent
8
- Requires-Python: >=3.6
9
- Requires-Dist: rich
10
- Requires-Dist: portalocker
11
- Dynamic: classifier
12
- Dynamic: requires-dist
13
- Dynamic: requires-python
14
- Dynamic: summary
@@ -1,7 +0,0 @@
1
- mlog_util/__init__.py,sha256=ZzcwRSCJDIHq_enQIRpvl8VRZz7izkL4VsnupRsR3AY,144
2
- mlog_util/handlers.py,sha256=0EXqAlzxVlcfXEO478EdYJSDdmlZZp5lRame4hEGnmA,10638
3
- mlog_util/log_manager.py,sha256=r4701jnTosT78lOVJzKnHGnqKnb5D3Yxq0VoN1oF2TI,4276
4
- mlog_util-0.1.3.dist-info/METADATA,sha256=4qRMzTajA6aXuChtgqEC9Z3X24zd6Ysxxt4XZBoZcMk,379
5
- mlog_util-0.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
- mlog_util-0.1.3.dist-info/top_level.txt,sha256=-liqOVoloTs4GLglhy_pzBBpK-ltsV3IZpg0OQsoD_4,10
7
- mlog_util-0.1.3.dist-info/RECORD,,