liblogging 0.1.2__tar.gz → 0.1.5__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: liblogging
3
- Version: 0.1.2
3
+ Version: 0.1.5
4
4
  Summary: Utilities for logging and sending logs.
5
5
  Home-page: https://github.com/XoriieInpottn/liblogging
6
6
  Author: xi
@@ -185,11 +185,19 @@ class Logger(logging.Logger):
185
185
  console_handler.setFormatter(formatter)
186
186
  self.addHandler(console_handler)
187
187
 
188
- def track_start(self, message: Union[str, Mapping], message_type: str = "on_track_start"):
189
- self._log(logging.INFO, {"message": message, "message_type": message_type}, (), stacklevel=2)
188
+ def track_start(
189
+ self, message: Union[str, Mapping], message_type: str = "on_track_start", stacklevel: int = 2, **kwargs
190
+ ):
191
+ self._log(
192
+ logging.INFO, {"message": message, "message_type": message_type, **kwargs}, (), stacklevel=stacklevel
193
+ )
190
194
 
191
- def track_end(self, message: Union[str, Mapping], message_type: str = "on_track_end"):
192
- self._log(logging.INFO, {"message": message, "message_type": message_type}, (), stacklevel=2)
195
+ def track_end(
196
+ self, message: Union[str, Mapping], message_type: str = "on_track_end", stacklevel: int = 2, **kwargs
197
+ ):
198
+ self._log(
199
+ logging.INFO, {"message": message, "message_type": message_type, **kwargs}, (), stacklevel=stacklevel
200
+ )
193
201
 
194
202
  def service_start(self):
195
203
  self._log(logging.INFO, {"message": "service_start", "message_type": "on_service_start"}, (), stacklevel=2)
@@ -197,22 +205,26 @@ class Logger(logging.Logger):
197
205
  def service_end(self):
198
206
  self._log(logging.INFO, {"message": "service_end", "message_type": "on_service_end"}, (), stacklevel=2)
199
207
 
200
- def turn_start(self, request: Mapping):
201
- self._log(logging.INFO, {"message": request, "message_type": "on_turn_start"}, (), stacklevel=2)
208
+ def turn_start(self, request: Mapping, **kwargs):
209
+ self.track_start(message=request, message_type="on_turn_start", stacklevel=3, **kwargs)
202
210
 
203
- def turn_end(self, response: Mapping):
204
- self._log(logging.INFO, {"message": response, "message_type": "on_turn_end"}, (), stacklevel=2)
211
+ def turn_end(self, response: Mapping, **kwargs):
212
+ self.track_end(message=response, message_type="on_turn_end", stacklevel=3, **kwargs)
205
213
 
206
- def tool_start(self, tool_name: str, inputs: Mapping):
214
+ def tool_start(self, tool_name: str, inputs: Mapping, **kwargs):
207
215
  self.track_start(
208
- message={"message": {"func_name": tool_name, "inputs": inputs}},
209
- message_type="on_tool_start"
216
+ message={"func_name": tool_name, "inputs": inputs},
217
+ message_type="on_tool_start",
218
+ stacklevel=3,
219
+ **kwargs
210
220
  )
211
221
 
212
- def tool_end(self, tool_name: str, output: Mapping, execute_time: float):
222
+ def tool_end(self, tool_name: str, output: Mapping, execute_time: float, **kwargs):
213
223
  self.track_end(
214
224
  message={"func_name": tool_name, "output": output, "duration": round(execute_time, 3)},
215
- message_type="on_tool_end"
225
+ message_type="on_tool_end",
226
+ stacklevel=3,
227
+ **kwargs
216
228
  )
217
229
 
218
230
  def llm_start(
@@ -220,7 +232,8 @@ class Logger(logging.Logger):
220
232
  llm_chain_name: str,
221
233
  messages: Sequence[Mapping],
222
234
  template_info: Mapping,
223
- model_kwargs: Mapping = None
235
+ model_kwargs: Mapping = None,
236
+ **kwargs
224
237
  ):
225
238
  log_info_dict = {
226
239
  "func_name": llm_chain_name,
@@ -230,7 +243,9 @@ class Logger(logging.Logger):
230
243
  }
231
244
  self.track_start(
232
245
  message=log_info_dict,
233
- message_type="on_llm_start"
246
+ message_type="on_llm_start",
247
+ stacklevel=3,
248
+ **kwargs
234
249
  )
235
250
 
236
251
  def llm_end(
@@ -240,7 +255,8 @@ class Logger(logging.Logger):
240
255
  execute_time: float,
241
256
  completion_tokens: int = None,
242
257
  prompt_tokens: int = None,
243
- role: str = "assistant"
258
+ role: str = "assistant",
259
+ **kwargs
244
260
  ):
245
261
  log_info_dict = {
246
262
  "func_name": llm_name,
@@ -249,10 +265,10 @@ class Logger(logging.Logger):
249
265
  "prompt_tokens": prompt_tokens,
250
266
  "duration": round(execute_time, 3)
251
267
  }
252
- self.track_end(message=log_info_dict, message_type="on_llm_end")
268
+ self.track_end(message=log_info_dict, message_type="on_llm_end", stacklevel=3, **kwargs)
253
269
 
254
- def agent(self, message: Union[str, Mapping]):
255
- self._log(logging.INFO, {"message": message, "message_type": "agent"}, ())
270
+ def agent(self, message: Union[str, Mapping], **kwargs):
271
+ self._log(logging.INFO, {"message": message, "message_type": "agent", **kwargs}, ())
256
272
 
257
273
 
258
274
  logger = Logger("libentry.logger")
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env python3
2
+
3
+ __author__ = "yubin"
4
+ __all__ = [
5
+ "get_trace_id",
6
+ "split_trace_id",
7
+ ]
8
+
9
+ import uuid
10
+ from typing import Dict, Union, Tuple
11
+ from datetime import datetime
12
+
13
+
14
+ def get_trace_id(
15
+ request: Union[object, Dict], add_timestamp: bool = True, combine_symbol: str = "+"
16
+ ) -> str:
17
+ """
18
+ 获取trace_id
19
+
20
+ 参数:
21
+ request (Union[Dict, object]): 请求对象,可以是包含 'uid', 'session_id', 'turn' 属性的对象或包含这些键的字典
22
+ add_timestamp (bool): 是否添加时间戳
23
+ combine_symbol (str): 各个部分的连接符
24
+
25
+ 返回:
26
+ str: 生成的trace_id
27
+ """
28
+ try:
29
+ if hasattr(request, "uid") and hasattr(request, "session_id") and hasattr(request, "turn"):
30
+ combine = [request.uid, request.session_id, str(request.turn)]
31
+ elif isinstance(request, Dict):
32
+ combine = [request.get("uid"), request.get("session_id"), str(request.get("turn"))]
33
+ else:
34
+ raise TypeError("Unsupported request type")
35
+ if add_timestamp:
36
+ timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
37
+ combine.append(timestamp)
38
+ result = combine_symbol.join(combine)
39
+ except (AttributeError, TypeError) as e:
40
+ print(f"Error occurred: {e}")
41
+ result = str(uuid.uuid4())
42
+ return result
43
+
44
+
45
+ def split_trace_id(trace_id: str, combine_symbol: str = "+") -> Tuple:
46
+ """
47
+ 拆分trace_id
48
+
49
+ 参数:
50
+ trace_id (str): 需要拆分的trace_id
51
+ combine_symbol (str): 各个部分的连接符
52
+
53
+ 返回:
54
+ tuple: 包含原始trace_id和一个字典,字典包含 'uid', 'session_id', 'turn' 键,如果无法正确拆分则返回空字典
55
+ """
56
+ parts = trace_id.split(combine_symbol)
57
+ if len(parts) == 3 or len(parts) == 4: # 包含时间戳时长度为4
58
+ try:
59
+ uid, session_id, turn = parts[:3]
60
+ return trace_id, {"uid": uid, "session_id": session_id, "turn": turn}
61
+ except ValueError:
62
+ pass
63
+ return trace_id, {}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: liblogging
3
- Version: 0.1.2
3
+ Version: 0.1.5
4
4
  Summary: Utilities for logging and sending logs.
5
5
  Home-page: https://github.com/XoriieInpottn/liblogging
6
6
  Author: xi
@@ -3,6 +3,7 @@ README.md
3
3
  setup.py
4
4
  liblogging/__init__.py
5
5
  liblogging/logger.py
6
+ liblogging/util.py
6
7
  liblogging.egg-info/PKG-INFO
7
8
  liblogging.egg-info/SOURCES.txt
8
9
  liblogging.egg-info/dependency_links.txt
@@ -12,7 +12,7 @@ if __name__ == '__main__':
12
12
  packages=[
13
13
  'liblogging',
14
14
  ],
15
- version='0.1.2',
15
+ version='0.1.5',
16
16
  description='Utilities for logging and sending logs.',
17
17
  long_description_content_type='text/markdown',
18
18
  long_description=long_description,
File without changes
File without changes
File without changes