xmi-logger 0.0.4__tar.gz → 0.0.6__tar.gz
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.
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/PKG-INFO +1 -1
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/setup.py +1 -1
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger/xmi_logger.py +25 -24
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger.egg-info/PKG-INFO +1 -1
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/README.md +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/setup.cfg +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger/__init__.py +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger/advanced_features.py +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger.egg-info/SOURCES.txt +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger.egg-info/dependency_links.txt +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger.egg-info/requires.txt +0 -0
- {xmi_logger-0.0.4 → xmi_logger-0.0.6}/xmi_logger.egg-info/top_level.txt +0 -0
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
import os
|
|
10
10
|
import sys
|
|
11
|
+
import time
|
|
11
12
|
import inspect
|
|
12
13
|
import requests
|
|
13
14
|
import asyncio
|
|
@@ -31,14 +32,14 @@ class XmiLogger:
|
|
|
31
32
|
基于 Loguru 的增强日志记录器,具有以下功能:
|
|
32
33
|
- 自定义日志格式
|
|
33
34
|
- 日志轮转和保留策略
|
|
34
|
-
-
|
|
35
|
-
-
|
|
35
|
+
- 上下文信息管理(如 request_id)
|
|
36
|
+
- 远程日志收集(使用线程池防止阻塞)
|
|
36
37
|
- 装饰器用于记录函数调用和执行时间,支持同步/异步函数
|
|
37
|
-
-
|
|
38
|
+
- 自定义日志级别(避免与 Loguru 预定义的冲突)
|
|
38
39
|
- 统一异常处理
|
|
39
40
|
|
|
40
41
|
新增:
|
|
41
|
-
-
|
|
42
|
+
- 可指定语言(中文/英文),默认中文
|
|
42
43
|
- 支持按时间轮转日志
|
|
43
44
|
- 支持自定义日志格式
|
|
44
45
|
- 支持日志级别过滤
|
|
@@ -86,35 +87,35 @@ class XmiLogger:
|
|
|
86
87
|
_location_cache: Dict[str, str] = {}
|
|
87
88
|
_stats_cache: Dict[str, Any] = {}
|
|
88
89
|
_stats_cache_time = 0
|
|
89
|
-
_stats_cache_ttl = 5 # 统计缓存TTL
|
|
90
|
+
_stats_cache_ttl = 5 # 统计缓存TTL(秒)
|
|
90
91
|
|
|
91
92
|
def __init__(
|
|
92
93
|
self,
|
|
93
94
|
file_name: str,
|
|
94
95
|
log_dir: str = 'logs',
|
|
95
|
-
max_size: int = 14,
|
|
96
|
+
max_size: int = 14, # 单位:MB
|
|
96
97
|
retention: str = '7 days',
|
|
97
98
|
remote_log_url: Optional[str] = None,
|
|
98
99
|
max_workers: int = 3,
|
|
99
100
|
work_type: bool = False,
|
|
100
|
-
language: str = 'zh',
|
|
101
|
-
rotation_time: Optional[str] = None,
|
|
102
|
-
custom_format: Optional[str] = None,
|
|
103
|
-
filter_level: str = "DEBUG",
|
|
104
|
-
compression: str = "zip",
|
|
105
|
-
enable_stats: bool = False,
|
|
106
|
-
categories: Optional[list] = None,
|
|
107
|
-
cache_size: int = 128,
|
|
108
|
-
adaptive_level: bool = False,
|
|
109
|
-
performance_mode: bool = False,
|
|
101
|
+
language: str = 'zh', # 语言选项,默认为中文
|
|
102
|
+
rotation_time: Optional[str] = None, # 新增:按时间轮转,如 "1 day", "1 week"
|
|
103
|
+
custom_format: Optional[str] = None, # 新增:自定义日志格式
|
|
104
|
+
filter_level: str = "DEBUG", # 新增:日志过滤级别
|
|
105
|
+
compression: str = "zip", # 新增:压缩格式,支持 zip, gz, tar
|
|
106
|
+
enable_stats: bool = False, # 新增:是否启用日志统计
|
|
107
|
+
categories: Optional[list] = None, # 新增:日志分类列表
|
|
108
|
+
cache_size: int = 128, # 新增:缓存大小配置
|
|
109
|
+
adaptive_level: bool = False, # 新增:自适应日志级别
|
|
110
|
+
performance_mode: bool = False, # 新增:性能模式
|
|
110
111
|
) -> None:
|
|
111
112
|
"""
|
|
112
113
|
初始化日志记录器。
|
|
113
114
|
|
|
114
115
|
Args:
|
|
115
|
-
file_name (str):
|
|
116
|
+
file_name (str): 日志文件名称(主日志文件前缀)。
|
|
116
117
|
log_dir (str): 日志文件目录。
|
|
117
|
-
max_size (int):
|
|
118
|
+
max_size (int): 日志文件大小(MB)超过时进行轮转。
|
|
118
119
|
retention (str): 日志保留策略。
|
|
119
120
|
remote_log_url (str, optional): 远程日志收集的URL。如果提供,将启用远程日志收集。
|
|
120
121
|
max_workers (int): 线程池的最大工作线程数。
|
|
@@ -257,7 +258,7 @@ class XmiLogger:
|
|
|
257
258
|
# 添加文件处理器
|
|
258
259
|
self._add_file_handlers(log_format)
|
|
259
260
|
|
|
260
|
-
#
|
|
261
|
+
# 配置远程日志(如果启用)
|
|
261
262
|
if self.remote_log_url:
|
|
262
263
|
self._configure_remote_logging()
|
|
263
264
|
|
|
@@ -422,7 +423,7 @@ class XmiLogger:
|
|
|
422
423
|
|
|
423
424
|
# 获取错误发生的具体位置
|
|
424
425
|
if tb:
|
|
425
|
-
#
|
|
426
|
+
# 获取最后一个调用帧(通常是错误发生的地方)
|
|
426
427
|
last_frame = tb[-1]
|
|
427
428
|
error_location = f"{last_frame.filename}:{last_frame.lineno}:{last_frame.name}"
|
|
428
429
|
line_content = last_frame.line.strip() if last_frame.line else "未知代码行"
|
|
@@ -612,9 +613,9 @@ class XmiLogger:
|
|
|
612
613
|
增强版日志装饰器,支持自定义日志级别和跟踪配置
|
|
613
614
|
|
|
614
615
|
Args:
|
|
615
|
-
msg (str): 支持多语言的异常提示信息key
|
|
616
|
-
level (str):
|
|
617
|
-
trace (bool):
|
|
616
|
+
msg (str): 支持多语言的异常提示信息key(使用_LANG_MAP中的键)
|
|
617
|
+
level (str): 记录异常的日志级别(默认ERROR)
|
|
618
|
+
trace (bool): 是否记录完整堆栈跟踪(默认True)
|
|
618
619
|
"""
|
|
619
620
|
def decorator(func):
|
|
620
621
|
_msg_key = msg or 'UNHANDLED_EXCEPTION'
|
|
@@ -673,7 +674,7 @@ class XmiLogger:
|
|
|
673
674
|
|
|
674
675
|
# 获取错误发生的具体位置
|
|
675
676
|
if tb:
|
|
676
|
-
#
|
|
677
|
+
# 获取最后一个调用帧(通常是错误发生的地方)
|
|
677
678
|
last_frame = tb[-1]
|
|
678
679
|
error_location = f"{last_frame.filename}:{last_frame.lineno}:{last_frame.name}"
|
|
679
680
|
line_content = last_frame.line.strip() if last_frame.line else "未知代码行"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|