sycommon-python-lib 0.1.56b6__py3-none-any.whl → 0.1.56b8__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.
Files changed (27) hide show
  1. sycommon/config/Config.py +17 -3
  2. sycommon/config/SentryConfig.py +13 -0
  3. sycommon/logging/kafka_log.py +185 -432
  4. sycommon/middleware/exception.py +10 -16
  5. sycommon/middleware/timeout.py +2 -1
  6. sycommon/middleware/traceid.py +67 -61
  7. sycommon/notice/uvicorn_monitor.py +32 -27
  8. sycommon/rabbitmq/rabbitmq_service.py +25 -854
  9. sycommon/rabbitmq/rabbitmq_service_client_manager.py +212 -0
  10. sycommon/rabbitmq/rabbitmq_service_connection_monitor.py +73 -0
  11. sycommon/rabbitmq/rabbitmq_service_consumer_manager.py +283 -0
  12. sycommon/rabbitmq/rabbitmq_service_core.py +117 -0
  13. sycommon/rabbitmq/rabbitmq_service_producer_manager.py +235 -0
  14. sycommon/sentry/__init__.py +0 -0
  15. sycommon/sentry/sy_sentry.py +34 -0
  16. sycommon/services.py +4 -0
  17. sycommon/synacos/nacos_client_base.py +119 -0
  18. sycommon/synacos/nacos_config_manager.py +106 -0
  19. sycommon/synacos/nacos_heartbeat_manager.py +142 -0
  20. sycommon/synacos/nacos_service.py +59 -780
  21. sycommon/synacos/nacos_service_discovery.py +138 -0
  22. sycommon/synacos/nacos_service_registration.py +252 -0
  23. {sycommon_python_lib-0.1.56b6.dist-info → sycommon_python_lib-0.1.56b8.dist-info}/METADATA +2 -1
  24. {sycommon_python_lib-0.1.56b6.dist-info → sycommon_python_lib-0.1.56b8.dist-info}/RECORD +27 -14
  25. {sycommon_python_lib-0.1.56b6.dist-info → sycommon_python_lib-0.1.56b8.dist-info}/WHEEL +0 -0
  26. {sycommon_python_lib-0.1.56b6.dist-info → sycommon_python_lib-0.1.56b8.dist-info}/entry_points.txt +0 -0
  27. {sycommon_python_lib-0.1.56b6.dist-info → sycommon_python_lib-0.1.56b8.dist-info}/top_level.txt +0 -0
@@ -1,19 +1,15 @@
1
1
  import os
2
- import pprint
3
2
  import sys
4
- import traceback
5
- import asyncio
6
- import atexit
7
- from datetime import datetime
8
3
  import json
9
- import re
10
4
  import socket
11
- import time
12
5
  import threading
13
- from queue import Queue, Full, Empty
6
+ import traceback
7
+ import asyncio
8
+ from datetime import datetime
9
+
14
10
  from kafka import KafkaProducer
15
11
  from loguru import logger
16
- import loguru
12
+
17
13
  from sycommon.config.Config import Config, SingletonMeta
18
14
  from sycommon.middleware.context import current_trace_id, current_headers
19
15
  from sycommon.tools.snowflake import Snowflake
@@ -27,418 +23,201 @@ LOGURU_FORMAT = (
27
23
  )
28
24
 
29
25
 
30
- class KafkaLogger(metaclass=SingletonMeta):
31
- _producer = None
32
- _topic = None
33
- _service_id = None
34
- _log_queue = Queue(maxsize=10000)
35
- _stop_event = threading.Event()
36
- _sender_thread = None
37
- _log_pattern = re.compile(
38
- r'^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+)\s*\|\s*(\w+)\s*\|\s*(\S+):(\S+):(\d+)\s*-\s*(\{.*\})\s*$'
39
- )
40
- _queue_warning_threshold = 9000
41
- _queue_warning_interval = 60 # 秒
42
- _last_queue_warning = 0
43
- _shutdown_timeout = 15 # 关闭超时时间,秒
44
- _config = None # 配置变量存储
45
-
46
- @staticmethod
47
- def setup_logger(config: dict):
48
- # 保存配置到类变量
49
- KafkaLogger._config = config
26
+ class KafkaSink:
27
+ """
28
+ 自定义 Loguru Sink,负责格式化日志并发送到 Kafka
29
+ """
50
30
 
31
+ def __init__(self, service_id: str):
32
+ self.service_id = service_id
33
+ # 获取配置
51
34
  from sycommon.synacos.nacos_service import NacosService
52
- KafkaLogger._topic = "shengye-json-log"
53
- KafkaLogger._service_id = NacosService(config).service_name
54
-
55
- # 获取 common 配置
56
- common = NacosService(config).share_configs.get("common.yml", {})
35
+ common = NacosService(
36
+ Config().config).share_configs.get("common.yml", {})
57
37
  bootstrap_servers = common.get("log", {}).get(
58
38
  "kafka", {}).get("servers", None)
59
39
 
60
- # 创建生产者,优化配置参数
61
- KafkaLogger._producer = KafkaProducer(
40
+ self._producer = KafkaProducer(
62
41
  bootstrap_servers=bootstrap_servers,
63
42
  value_serializer=lambda v: json.dumps(
64
43
  v, ensure_ascii=False).encode('utf-8'),
65
- max_block_ms=60000, # 增加最大阻塞时间从30秒到60秒
66
- retries=10, # 增加重试次数从5次到10次
67
- request_timeout_ms=30000, # 增加请求超时时间从10秒到30秒
68
- compression_type='gzip', # 添加压缩以减少网络传输量
69
- batch_size=16384, # 增大批处理大小
70
- linger_ms=5, # 添加短暂延迟以允许更多消息批处理
71
- buffer_memory=67108864, # 增大缓冲区内存
72
- connections_max_idle_ms=540000, # 连接最大空闲时间
73
- reconnect_backoff_max_ms=10000, # 增加重连退避最大时间
74
- max_in_flight_requests_per_connection=1, # 限制单个连接上未确认的请求数量
75
- # enable_idempotence=True, # 开启幂等性
44
+ # 保持原有的优化配置
45
+ max_block_ms=60000,
46
+ retries=5,
47
+ request_timeout_ms=30000,
48
+ compression_type='gzip',
49
+ batch_size=16384,
50
+ linger_ms=5,
51
+ buffer_memory=33554432,
76
52
  )
77
53
 
78
- # 启动后台发送线程
79
- KafkaLogger._sender_thread = threading.Thread(
80
- target=KafkaLogger._send_logs,
81
- daemon=True
82
- )
83
- KafkaLogger._sender_thread.start()
54
+ def write(self, message):
55
+ """
56
+ Loguru 会调用此方法。
57
+ message 参数实际上是 loguru.Message 对象,可以通过 message.record 获取所有字段。
58
+ """
59
+ try:
60
+ # 1. 获取原始日志记录
61
+ record = message.record
84
62
 
85
- # 注册退出处理
86
- atexit.register(KafkaLogger.close)
63
+ # 2. 提取 TraceID
64
+ trace_id = None
65
+ try:
66
+ # 如果业务方传的是 JSON 字符串作为 message
67
+ msg_obj = json.loads(record["message"])
68
+ if isinstance(msg_obj, dict):
69
+ trace_id = msg_obj.get("trace_id")
70
+ except:
71
+ pass
72
+
73
+ if not trace_id:
74
+ trace_id = current_trace_id.get()
75
+
76
+ if not trace_id:
77
+ trace_id = str(Snowflake.id)
78
+ else:
79
+ trace_id = str(trace_id)
80
+
81
+ # 3. 提取异常详情 (如果有)
82
+ error_detail = ""
83
+ if record["exception"] is not None:
84
+ # Loguru 的 exception 对象
85
+ error_detail = "".join(traceback.format_exception(
86
+ record["exception"].type,
87
+ record["exception"].value,
88
+ record["exception"].traceback
89
+ ))
90
+ elif "error" in record["extra"]:
91
+ # 兼容其他方式注入的异常
92
+ error_detail = str(record["extra"].get("error"))
93
+
94
+ # 4. 获取主机信息
95
+ try:
96
+ ip = socket.gethostbyname(socket.gethostname())
97
+ except:
98
+ ip = '127.0.0.1'
99
+ host_name = socket.gethostname()
87
100
 
88
- # 设置全局异常处理器
89
- sys.excepthook = KafkaLogger._handle_exception
101
+ # 5. 获取线程/协程信息
102
+ try:
103
+ task = asyncio.current_task()
104
+ thread_info = f"coroutine:{task.get_name()}" if task else f"thread:{threading.current_thread().name}"
105
+ except RuntimeError:
106
+ thread_info = f"thread:{threading.current_thread().name}"
107
+
108
+ # 6. 提取类名/文件名信息
109
+ file_name = record["file"].name
110
+ logger_name = record["name"]
111
+ if logger_name and logger_name != file_name:
112
+ class_name = f"{file_name}:{logger_name}"
113
+ else:
114
+ class_name = file_name
115
+
116
+ # 7. 构建最终的 Kafka 日志结构
117
+ log_entry = {
118
+ "traceId": trace_id,
119
+ "sySpanId": "",
120
+ "syBizId": "",
121
+ "ptxId": "",
122
+ "time": record["time"].strftime("%Y-%m-%d %H:%M:%S"),
123
+ "day": datetime.now().strftime("%Y.%m.%d"),
124
+ "msg": record["message"],
125
+ "detail": error_detail,
126
+ "ip": ip,
127
+ "hostName": host_name,
128
+ "tenantId": "",
129
+ "userId": "",
130
+ "customerId": "",
131
+ "env": Config().config.get('Nacos', {}).get('namespaceId', ''),
132
+ "priReqSource": "",
133
+ "reqSource": "",
134
+ "serviceId": self.service_id,
135
+ "logLevel": record["level"].name,
136
+ "className": class_name,
137
+ "method": record["function"],
138
+ "line": str(record["line"]),
139
+ "theadName": thread_info,
140
+ "sqlCost": 0,
141
+ "size": len(str(record["message"])),
142
+ "uid": int(Snowflake.id)
143
+ }
144
+
145
+ # 8. 发送
146
+ self._producer.send("shengye-json-log", log_entry)
147
+
148
+ except Exception as e:
149
+ print(f"KafkaSink Error: {e}")
150
+
151
+ def flush(self):
152
+ if self._producer:
153
+ self._producer.flush(timeout=5)
90
154
 
91
- def custom_log_handler(record):
92
- # 检查record是否是Message对象
93
- if isinstance(record, loguru._handler.Message):
94
- # 从Message对象中获取原始日志记录
95
- record = record.record
96
-
97
- # 提取基本信息
98
- message = record["message"]
99
- level = record["level"].name
100
- time_str = record["time"].strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
101
-
102
- # 提取文件、函数和行号信息
103
- file_info = record["file"].name
104
- function_info = record["function"]
105
- line_info = record["line"]
106
-
107
- # 尝试从message中提取trace_id
108
- trace_id = None
109
- try:
110
- if isinstance(message, str):
111
- msg_dict = json.loads(message)
112
- trace_id = msg_dict.get("trace_id")
113
- except json.JSONDecodeError:
114
- trace_id = None
115
-
116
- if not trace_id:
117
- trace_id = SYLogger.get_trace_id() or Snowflake.id
118
-
119
- # 获取线程/协程信息
120
- thread_info = SYLogger._get_execution_context()
121
-
122
- # 获取主机信息
123
- try:
124
- ip = socket.gethostbyname(socket.gethostname())
125
- except socket.gaierror:
126
- ip = '127.0.0.1'
127
- host_name = socket.gethostname()
128
-
129
- # 检查是否有错误信息并设置detail字段
130
- error_detail = ""
131
- if level == "ERROR" and record["exception"] is not None:
132
- error_detail = "".join(traceback.format_exception(
133
- record["exception"].type,
134
- record["exception"].value,
135
- record["exception"].traceback
136
- ))
137
-
138
- # 获取logger名称作为类名
139
- class_name = record["name"]
140
-
141
- # 合并文件名和类名信息
142
- if file_info and class_name:
143
- full_class_name = f"{file_info}:{class_name}"
144
- elif file_info:
145
- full_class_name = file_info
146
- else:
147
- full_class_name = class_name
148
-
149
- # 构建日志条目
150
- log_entry = {
151
- "traceId": trace_id,
152
- "sySpanId": "",
153
- "syBizId": "",
154
- "ptxId": "",
155
- "time": time_str,
156
- "day": datetime.now().strftime("%Y.%m.%d"),
157
- "msg": message,
158
- "detail": error_detail,
159
- "ip": ip,
160
- "hostName": host_name,
161
- "tenantId": "",
162
- "userId": "",
163
- "customerId": "",
164
- "env": Config().config['Nacos']['namespaceId'],
165
- "priReqSource": "",
166
- "reqSource": "",
167
- "serviceId": KafkaLogger._service_id,
168
- "logLevel": level,
169
- "classShortName": "",
170
- "method": "",
171
- "line": "",
172
- "theadName": thread_info,
173
- "className": "",
174
- "sqlCost": 0,
175
- "size": len(str(message)),
176
- "uid": int(Snowflake.id) # 独立新的id
177
- }
178
-
179
- # 智能队列管理
180
- if not KafkaLogger._safe_put_to_queue(log_entry):
181
- logger.warning(json.dumps({
182
- "trace_id": trace_id,
183
- "message": "Log queue is full, log discarded",
184
- "level": "WARNING"
185
- }, ensure_ascii=False))
186
-
187
- # 配置日志处理器
155
+
156
+ class KafkaLogger(metaclass=SingletonMeta):
157
+ _sink_instance = None
158
+
159
+ @staticmethod
160
+ def setup_logger(config: dict):
188
161
  logger.remove()
189
162
 
190
- # 添加Kafka日志处理器
163
+ from sycommon.synacos.nacos_service import NacosService
164
+ service_id = NacosService(config).service_name
165
+
166
+ KafkaLogger._sink_instance = KafkaSink(service_id)
167
+
191
168
  logger.add(
192
- custom_log_handler,
169
+ KafkaLogger._sink_instance,
193
170
  level="INFO",
194
- enqueue=True # 使用Loguru的队列功能
171
+ format="{message}",
172
+ enqueue=True,
173
+ backtrace=True,
174
+ diagnose=True
195
175
  )
196
176
 
197
- # 添加控制台错误日志处理器
198
177
  logger.add(
199
178
  sink=sys.stdout,
200
179
  level="ERROR",
201
180
  format=LOGURU_FORMAT,
202
- colorize=True, # 启用颜色
203
- filter=lambda record: record["level"].name == "ERROR"
181
+ colorize=True,
182
+ backtrace=True,
183
+ diagnose=True
204
184
  )
205
185
 
186
+ sys.excepthook = KafkaLogger._handle_exception
187
+
206
188
  @staticmethod
207
189
  def _handle_exception(exc_type, exc_value, exc_traceback):
208
- """全局异常处理器"""
209
- # 跳过键盘中断(Ctrl+C)
210
190
  if issubclass(exc_type, KeyboardInterrupt):
211
191
  sys.__excepthook__(exc_type, exc_value, exc_traceback)
212
192
  return
213
193
 
214
- # 获取当前的trace_id
215
- trace_id = SYLogger.get_trace_id() or Snowflake.id
216
-
217
- # 构建错误日志
218
- error_log = {
194
+ trace_id = current_trace_id.get() or str(Snowflake.id)
195
+ error_msg = json.dumps({
219
196
  "trace_id": trace_id,
220
- "message": f"Uncaught exception: {exc_type.__name__}: {str(exc_value)}",
221
- "level": "ERROR",
222
- "detail": "".join(traceback.format_exception(exc_type, exc_value, exc_traceback))
223
- }
197
+ "message": f"Uncaught exception: {exc_type.__name__}",
198
+ "level": "ERROR"
199
+ }, ensure_ascii=False)
224
200
 
225
- # 使用Loguru记录错误,确保包含完整堆栈跟踪
226
- logger.opt(exception=(exc_type, exc_value, exc_traceback)).error(
227
- json.dumps(error_log, ensure_ascii=False)
228
- )
229
-
230
- @staticmethod
231
- def _safe_put_to_queue(log_entry):
232
- """安全放入队列,提供更健壮的队列管理"""
233
- try:
234
- # 检查队列水位并发出警告
235
- current_time = time.time()
236
- qsize = KafkaLogger._log_queue.qsize()
237
-
238
- if qsize > KafkaLogger._queue_warning_threshold:
239
- if current_time - KafkaLogger._last_queue_warning > KafkaLogger._queue_warning_interval:
240
- warning_msg = f"Log queue at {qsize}/{KafkaLogger._log_queue.maxsize} capacity"
241
- print(warning_msg)
242
- logger.warning(json.dumps({
243
- "trace_id": log_entry.get("traceId"),
244
- "message": warning_msg,
245
- "level": "WARNING"
246
- }, ensure_ascii=False))
247
- KafkaLogger._last_queue_warning = current_time
248
-
249
- # 尝试快速放入
250
- KafkaLogger._log_queue.put(log_entry, block=False)
251
- return True
252
- except Full:
253
- # 队列已满时的处理策略
254
- if KafkaLogger._stop_event.is_set():
255
- # 关闭过程中直接丢弃日志
256
- return False
257
-
258
- # 尝试移除最旧的日志并添加新日志
259
- try:
260
- with threading.Lock(): # 添加锁确保操作原子性
261
- if not KafkaLogger._log_queue.empty():
262
- KafkaLogger._log_queue.get_nowait()
263
- KafkaLogger._log_queue.put_nowait(log_entry)
264
- return True
265
- except Exception:
266
- return False
267
-
268
- @staticmethod
269
- def _send_logs():
270
- """后台线程:批量发送日志到Kafka,优化内存使用"""
271
- batch = []
272
- last_flush = time.time()
273
- batch_size = 100
274
- flush_interval = 1 # 秒
275
- consecutive_errors = 0
276
- max_consecutive_errors = 10 # 最大连续错误数,超过则降低处理速度
277
- last_reconnect_attempt = 0
278
- reconnect_interval = 30 # 重新连接尝试间隔,秒
279
-
280
- while not KafkaLogger._stop_event.is_set():
281
- try:
282
- # 检查生产者状态,如果长时间失败,尝试重新创建生产者
283
- current_time = time.time()
284
- if consecutive_errors > max_consecutive_errors and current_time - last_reconnect_attempt > reconnect_interval:
285
- logger.warning(json.dumps({
286
- "trace_id": "system",
287
- "message": "尝试重新创建Kafka生产者以解决连接问题",
288
- "level": "WARNING"
289
- }, ensure_ascii=False))
290
- last_reconnect_attempt = current_time
291
-
292
- # 尝试重新创建生产者
293
- try:
294
- # 使用类变量中存储的配置
295
- from sycommon.synacos.nacos_service import NacosService
296
- common = NacosService(
297
- KafkaLogger._config).share_configs.get("common.yml", {})
298
- bootstrap_servers = common.get("log", {}).get(
299
- "kafka", {}).get("servers", None)
300
-
301
- # 关闭旧生产者
302
- if KafkaLogger._producer:
303
- KafkaLogger._producer.close(timeout=5)
304
-
305
- # 创建新生产者
306
- KafkaLogger._producer = KafkaProducer(
307
- bootstrap_servers=bootstrap_servers,
308
- value_serializer=lambda v: json.dumps(
309
- v, ensure_ascii=False).encode('utf-8'),
310
- max_block_ms=60000,
311
- retries=10,
312
- request_timeout_ms=30000,
313
- compression_type='gzip',
314
- batch_size=16384,
315
- linger_ms=5,
316
- buffer_memory=67108864,
317
- connections_max_idle_ms=540000,
318
- reconnect_backoff_max_ms=10000,
319
- )
320
- consecutive_errors = 0
321
- logger.info(json.dumps({
322
- "trace_id": "system",
323
- "message": "Kafka生产者已重新创建",
324
- "level": "INFO"
325
- }, ensure_ascii=False))
326
- except Exception as e:
327
- logger.error(json.dumps({
328
- "trace_id": "system",
329
- "message": f"重新创建Kafka生产者失败: {str(e)}",
330
- "level": "ERROR"
331
- }, ensure_ascii=False))
332
-
333
- # 批量获取日志
334
- while len(batch) < batch_size and not KafkaLogger._stop_event.is_set():
335
- try:
336
- # 使用超时获取,避免长时间阻塞
337
- log_entry = KafkaLogger._log_queue.get(timeout=0.5)
338
- batch.append(log_entry)
339
- except Empty:
340
- break
341
-
342
- # 定时或定量发送
343
- current_time = time.time()
344
- if batch and (len(batch) >= batch_size or (current_time - last_flush > flush_interval)):
345
- try:
346
- # 分批发送,避免一次发送过大
347
- sub_batch_size = min(50, batch_size)
348
- for i in range(0, len(batch), sub_batch_size):
349
- sub_batch = batch[i:i+sub_batch_size]
350
- for entry in sub_batch:
351
- KafkaLogger._producer.send(
352
- KafkaLogger._topic, entry)
353
- KafkaLogger._producer.flush(timeout=15)
354
-
355
- batch = [] # 发送成功后清空批次
356
- last_flush = current_time
357
- consecutive_errors = 0 # 重置错误计数
358
- except Exception as e:
359
- consecutive_errors += 1
360
- error_msg = f"Kafka发送失败: {e}"
361
- print(error_msg)
362
- logger.error(json.dumps({
363
- "trace_id": "system",
364
- "message": error_msg,
365
- "level": "ERROR"
366
- }, ensure_ascii=False))
367
-
368
- # 连续错误过多时增加休眠时间,避免CPU空转
369
- if consecutive_errors > max_consecutive_errors:
370
- sleep_time = min(5, consecutive_errors // 2)
371
- time.sleep(sleep_time)
372
-
373
- except Exception as e:
374
- print(f"日志处理线程异常: {e}")
375
- time.sleep(1) # 短暂休眠恢复
376
-
377
- # 退出前发送剩余日志
378
- if batch:
379
- try:
380
- for entry in batch:
381
- KafkaLogger._producer.send(KafkaLogger._topic, entry)
382
- KafkaLogger._producer.flush(
383
- timeout=KafkaLogger._shutdown_timeout)
384
- except Exception as e:
385
- print(f"关闭时发送剩余日志失败: {e}")
201
+ logger.opt(exception=(exc_type, exc_value,
202
+ exc_traceback)).error(error_msg)
386
203
 
387
204
  @staticmethod
388
205
  def close():
389
- """安全关闭资源,增强可靠性"""
390
- if KafkaLogger._stop_event.is_set():
391
- return
392
-
393
- print("开始关闭Kafka日志系统...")
394
- KafkaLogger._stop_event.set()
395
-
396
- # 等待发送线程结束
397
- if KafkaLogger._sender_thread and KafkaLogger._sender_thread.is_alive():
398
- print(f"等待日志发送线程结束,超时时间: {KafkaLogger._shutdown_timeout}秒")
399
- KafkaLogger._sender_thread.join(
400
- timeout=KafkaLogger._shutdown_timeout)
401
-
402
- # 如果线程仍在运行,强制终止(虽然daemon线程会自动终止,但这里显式处理)
403
- if KafkaLogger._sender_thread.is_alive():
404
- print("日志发送线程未能及时结束,将被强制终止")
405
-
406
- # 关闭生产者
407
- if KafkaLogger._producer:
408
- try:
409
- print("关闭Kafka生产者...")
410
- KafkaLogger._producer.close(
411
- timeout=KafkaLogger._shutdown_timeout)
412
- print("Kafka生产者已关闭")
413
- except Exception as e:
414
- print(f"关闭Kafka生产者失败: {e}")
415
-
416
- # 清空队列防止内存滞留
417
- remaining = 0
418
- while not KafkaLogger._log_queue.empty():
419
- try:
420
- KafkaLogger._log_queue.get_nowait()
421
- remaining += 1
422
- except Empty:
423
- break
424
-
425
- print(f"已清空日志队列,剩余日志数: {remaining}")
206
+ if KafkaLogger._sink_instance:
207
+ KafkaLogger._sink_instance.flush()
426
208
 
427
209
 
428
210
  class SYLogger:
429
211
  @staticmethod
430
212
  def get_trace_id():
431
- """从上下文中获取当前的 trace_id"""
432
213
  return current_trace_id.get()
433
214
 
434
215
  @staticmethod
435
216
  def set_trace_id(trace_id: str):
436
- """设置当前的 trace_id"""
437
217
  return current_trace_id.set(trace_id)
438
218
 
439
219
  @staticmethod
440
220
  def reset_trace_id(token):
441
- """重置当前的 trace_id"""
442
221
  current_trace_id.reset(token)
443
222
 
444
223
  @staticmethod
@@ -455,64 +234,59 @@ class SYLogger:
455
234
 
456
235
  @staticmethod
457
236
  def _get_execution_context() -> str:
458
- """获取当前执行上下文的线程或协程信息,返回格式化字符串"""
459
237
  try:
460
- # 尝试获取协程信息
461
238
  task = asyncio.current_task()
462
239
  if task:
463
- task_name = task.get_name()
464
- return f"coroutine:{task_name}"
240
+ return f"coroutine:{task.get_name()}"
465
241
  except RuntimeError:
466
- # 不在异步上下文中,获取线程信息
467
- thread = threading.current_thread()
468
- return f"thread:{thread.name}"
469
-
470
- return "unknown"
242
+ pass
243
+ return f"thread:{threading.current_thread().name}"
471
244
 
472
245
  @staticmethod
473
246
  def _log(msg: any, level: str = "INFO"):
474
- trace_id = SYLogger.get_trace_id() or Snowflake.id
475
-
247
+ """
248
+ 统一日志记录入口
249
+ 修复:手动提取堆栈信息并写入 message,确保 Kafka 能收到
250
+ """
251
+ # 序列化消息
476
252
  if isinstance(msg, dict) or isinstance(msg, list):
477
253
  msg_str = json.dumps(msg, ensure_ascii=False)
478
254
  else:
479
255
  msg_str = str(msg)
480
256
 
481
- # 获取执行上下文信息并格式化为字符串
482
- thread_info = SYLogger._get_execution_context()
257
+ # 构建基础日志字典
258
+ log_dict = {
259
+ "trace_id": str(SYLogger.get_trace_id() or Snowflake.id),
260
+ "message": msg_str,
261
+ "level": level,
262
+ "threadName": SYLogger._get_execution_context()
263
+ }
483
264
 
484
- # 构建日志结构,添加线程/协程信息到threadName字段
485
- request_log = {}
265
+ # 如果是 ERROR 级别,手动获取堆栈并加入 log_dict
486
266
  if level == "ERROR":
487
- request_log = {
488
- "trace_id": str(trace_id) if trace_id else Snowflake.id,
489
- "message": msg_str,
490
- "traceback": traceback.format_exc(),
491
- "level": level,
492
- "threadName": thread_info
493
- }
494
- else:
495
- request_log = {
496
- "trace_id": str(trace_id) if trace_id else Snowflake.id,
497
- "message": msg_str,
498
- "level": level,
499
- "threadName": thread_info
500
- }
267
+ # 获取当前异常信息 (sys.exc_info() 在 except 块中有效)
268
+ exc_info = sys.exc_info()
269
+ if exc_info and exc_info[0] is not None:
270
+ # 将堆栈格式化为字符串,放入 detail 字段
271
+ # 这样 KafkaSink 解析 message 时,就能拿到 detail
272
+ tb_str = "".join(traceback.format_exception(*exc_info))
273
+ log_dict["detail"] = tb_str
274
+
275
+ # 将字典转为 JSON 字符串传给 Loguru
276
+ log_json = json.dumps(log_dict, ensure_ascii=False)
501
277
 
502
- # 选择日志级别
503
- _log = ''
504
278
  if level == "ERROR":
505
- _log = json.dumps(request_log, ensure_ascii=False)
506
- logger.error(_log)
279
+ # 依然使用 opt(exception=True) 让控制台打印彩色堆栈
280
+ # 注意:Loguru 内部可能会忽略我们已经塞进去的 detail 字符串,
281
+ # 但这没关系,因为 KafkaSink 解析 message 字符串时会重新读取 detail
282
+ logger.opt(exception=True).error(log_json)
507
283
  elif level == "WARNING":
508
- _log = json.dumps(request_log, ensure_ascii=False)
509
- logger.warning(_log)
284
+ logger.warning(log_json)
510
285
  else:
511
- _log = json.dumps(request_log, ensure_ascii=False)
512
- logger.info(_log)
286
+ logger.info(log_json)
513
287
 
514
288
  if os.getenv('DEV-LOG', 'false').lower() == 'true':
515
- pprint.pprint(_log)
289
+ print(log_json)
516
290
 
517
291
  @staticmethod
518
292
  def info(msg: any, *args, **kwargs):
@@ -532,25 +306,4 @@ class SYLogger:
532
306
 
533
307
  @staticmethod
534
308
  def exception(msg: any, *args, **kwargs):
535
- """记录异常信息,包括完整堆栈"""
536
- trace_id = SYLogger.get_trace_id() or Snowflake.id
537
-
538
- if isinstance(msg, dict) or isinstance(msg, list):
539
- msg_str = json.dumps(msg, ensure_ascii=False)
540
- else:
541
- msg_str = str(msg)
542
-
543
- # 获取执行上下文信息
544
- thread_info = SYLogger._get_execution_context()
545
-
546
- # 构建包含异常堆栈的日志
547
- request_log = {
548
- "trace_id": str(trace_id) if trace_id else Snowflake.id,
549
- "message": msg_str,
550
- "level": "ERROR",
551
- "threadName": thread_info
552
- }
553
-
554
- # 使用Loguru记录完整异常堆栈
555
- logger.opt(exception=True).error(
556
- json.dumps(request_log, ensure_ascii=False))
309
+ SYLogger._log(msg, "ERROR")