1
1
from __future__ import annotations
2
2
3
3
import inspect
4
+ import sys
4
5
import time
5
- from typing import Any , Mapping
6
+ from typing import TYPE_CHECKING , Any , Mapping
7
+
8
+ if TYPE_CHECKING :
9
+ import logging
6
10
7
11
8
12
def _create_buffer_record (
9
13
level : int ,
10
14
msg : object ,
11
15
args : object ,
16
+ exc_info : logging ._ExcInfoType = None ,
17
+ stack_info : bool = False ,
12
18
extra : Mapping [str , object ] | None = None ,
13
19
) -> dict [str , Any ]:
20
+ """
21
+ Create a structured log record for buffering to save in buffer.
22
+
23
+ Parameters
24
+ ----------
25
+ level : int
26
+ Logging level (e.g., logging.DEBUG, logging.INFO) indicating log severity.
27
+ msg : object
28
+ The log message to be recorded.
29
+ args : object
30
+ Additional arguments associated with the log message.
31
+ exc_info : logging._ExcInfoType, optional
32
+ Exception information to be included in the log record.
33
+ If None, no exception details will be captured.
34
+ stack_info : bool, default False
35
+ Flag to include stack trace information in the log record.
36
+ extra : Mapping[str, object], optional
37
+ Additional context or metadata to be attached to the log record.
38
+
39
+ Returns
40
+ -------
41
+ dict[str, Any]
42
+
43
+ Notes
44
+ -----
45
+ - Captures caller frame information for precise log source tracking
46
+ - Automatically handles exception context
47
+ """
48
+ # Retrieve the caller's frame information to capture precise log context
49
+ # Uses inspect.stack() with index 3 to get the original caller's details
14
50
caller_frame = inspect .stack ()[3 ]
51
+
52
+ # Get the current timestamp
15
53
timestamp = time .time ()
16
54
55
+ # Dynamically replace exc_info with current system exception information
56
+ # This ensures the most recent exception is captured if available
57
+ if exc_info :
58
+ exc_info = sys .exc_info ()
59
+
60
+ # Construct and return the og record dictionary
17
61
return {
18
62
"level" : level ,
19
63
"msg" : msg ,
@@ -23,11 +67,47 @@ def _create_buffer_record(
23
67
"function" : caller_frame .function ,
24
68
"extra" : extra ,
25
69
"timestamp" : timestamp ,
70
+ "exc_info" : exc_info ,
71
+ "stack_info" : stack_info ,
26
72
}
27
73
28
74
29
75
def _check_minimum_buffer_log_level (buffer_log_level , current_log_level ):
30
- # Define log level mapping
76
+ """
77
+ Determine if the current log level meets or exceeds the buffer's minimum log level.
78
+
79
+ Compares log levels to decide whether a log message should be included in the buffer.
80
+
81
+ Parameters
82
+ ----------
83
+ buffer_log_level : str
84
+ Minimum log level configured for the buffer.
85
+ Must be one of: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'.
86
+ current_log_level : str
87
+ Log level of the current log message.
88
+ Must be one of: 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'.
89
+
90
+ Returns
91
+ -------
92
+ bool
93
+ True if the current log level is lower (more verbose) than the buffer's
94
+ minimum log level, indicating the message should be buffered.
95
+ False if the current log level is higher (less verbose) and should not be buffered.
96
+
97
+ Notes
98
+ -----
99
+ - Log levels are compared based on their numeric severity
100
+ - Conversion to uppercase ensures case-insensitive comparisons
101
+
102
+ Examples
103
+ --------
104
+ >>> _check_minimum_buffer_log_level('INFO', 'DEBUG')
105
+ True
106
+ >>> _check_minimum_buffer_log_level('ERROR', 'WARNING')
107
+ False
108
+ """
109
+ # Predefined log level mapping with numeric severity values
110
+ # Lower values indicate more verbose logging levels
31
111
log_levels = {
32
112
"DEBUG" : 10 ,
33
113
"INFO" : 20 ,
@@ -36,7 +116,8 @@ def _check_minimum_buffer_log_level(buffer_log_level, current_log_level):
36
116
"CRITICAL" : 50 ,
37
117
}
38
118
39
- # Convert string levels to numeric if needed
119
+ # Normalize input log levels to uppercase for consistent comparison
120
+ # Retrieve corresponding numeric log level values
40
121
buffer_level_num = log_levels .get (buffer_log_level .upper ())
41
122
current_level_num = log_levels .get (current_log_level .upper ())
42
123
0 commit comments