22
22
23
23
from ._meta import ECS_VERSION
24
24
from ._utils import (
25
- TYPE_CHECKING ,
26
25
collections_abc ,
27
26
de_dot ,
28
27
flatten_dict ,
31
30
merge_dicts ,
32
31
)
33
32
34
- if TYPE_CHECKING :
35
- from typing import Any , Callable , Dict , Optional , Sequence
33
+ from typing import Any , Callable , Dict , Optional , Sequence
36
34
37
- try :
38
- from typing import Literal , Union # type: ignore
39
- except ImportError :
40
- from typing_extensions import Literal , Union # type: ignore
35
+ try :
36
+ from typing import Literal , Union # type: ignore
37
+ except ImportError :
38
+ from typing_extensions import Literal , Union # type: ignore
41
39
42
40
43
41
# Load the attributes of a LogRecord so if some are
@@ -78,16 +76,15 @@ class StdlibFormatter(logging.Formatter):
78
76
converter = time .gmtime
79
77
80
78
def __init__ (
81
- self , # type: Any
82
- fmt = None , # type: Optional[str]
83
- datefmt = None , # type: Optional[str]
84
- style = "%" , # type: Union[Literal["%"], Literal["{"], Literal["$"]]
85
- validate = None , # type: Optional[bool]
86
- stack_trace_limit = None , # type: Optional[int]
87
- extra = None , # type: Optional[Dict[str, Any]]
88
- exclude_fields = (), # type: Sequence[str]
89
- ):
90
- # type: (...) -> None
79
+ self ,
80
+ fmt : Optional [str ] = None ,
81
+ datefmt : Optional [str ] = None ,
82
+ style : Union [Literal ["%" ], Literal ["{" ], Literal ["$" ]] = "%" ,
83
+ validate : Optional [bool ] = None ,
84
+ stack_trace_limit : Optional [int ] = None ,
85
+ extra : Optional [Dict [str , Any ]] = None ,
86
+ exclude_fields : Sequence [str ] = (),
87
+ ) -> None :
91
88
"""Initialize the ECS formatter.
92
89
93
90
:param int stack_trace_limit:
@@ -137,8 +134,7 @@ def __init__(
137
134
self ._exclude_fields = frozenset (exclude_fields )
138
135
self ._stack_trace_limit = stack_trace_limit
139
136
140
- def _record_error_type (self , record ):
141
- # type: (logging.LogRecord) -> Optional[str]
137
+ def _record_error_type (self , record : logging .LogRecord ) -> Optional [str ]:
142
138
exc_info = record .exc_info
143
139
if not exc_info :
144
140
# exc_info is either an iterable or bool. If it doesn't
@@ -151,8 +147,7 @@ def _record_error_type(self, record):
151
147
return exc_info [0 ].__name__
152
148
return None
153
149
154
- def _record_error_message (self , record ):
155
- # type: (logging.LogRecord) -> Optional[str]
150
+ def _record_error_message (self , record : logging .LogRecord ) -> Optional [str ]:
156
151
exc_info = record .exc_info
157
152
if not exc_info :
158
153
# exc_info is either an iterable or bool. If it doesn't
@@ -165,13 +160,11 @@ def _record_error_message(self, record):
165
160
return str (exc_info [1 ])
166
161
return None
167
162
168
- def format (self , record ):
169
- # type: (logging.LogRecord) -> str
163
+ def format (self , record : logging .LogRecord ) -> str :
170
164
result = self .format_to_ecs (record )
171
165
return json_dumps (result )
172
166
173
- def format_to_ecs (self , record ):
174
- # type: (logging.LogRecord) -> Dict[str, Any]
167
+ def format_to_ecs (self , record : logging .LogRecord ) -> Dict [str , Any ]:
175
168
"""Function that can be overridden to add additional fields to
176
169
(or remove fields from) the JSON before being dumped into a string.
177
170
@@ -185,7 +178,7 @@ def format_to_ecs(self, record):
185
178
return result
186
179
"""
187
180
188
- extractors = {
181
+ extractors : Dict [ str , Callable [[ logging . LogRecord ], Any ]] = {
189
182
"@timestamp" : self ._record_timestamp ,
190
183
"ecs.version" : lambda _ : ECS_VERSION ,
191
184
"log.level" : lambda r : (r .levelname .lower () if r .levelname else None ),
@@ -201,9 +194,9 @@ def format_to_ecs(self, record):
201
194
"error.type" : self ._record_error_type ,
202
195
"error.message" : self ._record_error_message ,
203
196
"error.stack_trace" : self ._record_error_stack_trace ,
204
- } # type: Dict[str, Callable[[logging.LogRecord],Any]]
197
+ }
205
198
206
- result = {} # type : Dict[str, Any]
199
+ result : Dict [str , Any ] = {}
207
200
for field in set (extractors .keys ()).difference (self ._exclude_fields ):
208
201
if self ._is_field_excluded (field ):
209
202
continue
@@ -257,28 +250,26 @@ def format_to_ecs(self, record):
257
250
return result
258
251
259
252
@lru_cache ()
260
- def _is_field_excluded (self , field ):
261
- # type: (str) -> bool
253
+ def _is_field_excluded (self , field : str ) -> bool :
262
254
field_path = []
263
255
for path in field .split ("." ):
264
256
field_path .append (path )
265
257
if "." .join (field_path ) in self ._exclude_fields :
266
258
return True
267
259
return False
268
260
269
- def _record_timestamp (self , record ):
270
- # type: (logging.LogRecord) -> str
261
+ def _record_timestamp (self , record : logging .LogRecord ) -> str :
271
262
return "%s.%03dZ" % (
272
263
self .formatTime (record , datefmt = "%Y-%m-%dT%H:%M:%S" ),
273
264
record .msecs ,
274
265
)
275
266
276
- def _record_attribute (self , attribute ):
277
- # type: (str) -> Callable[[logging.LogRecord], Optional[Any]]
267
+ def _record_attribute (
268
+ self , attribute : str
269
+ ) -> Callable [[logging .LogRecord ], Optional [Any ]]:
278
270
return lambda r : getattr (r , attribute , None )
279
271
280
- def _record_error_stack_trace (self , record ):
281
- # type: (logging.LogRecord) -> Optional[str]
272
+ def _record_error_stack_trace (self , record : logging .LogRecord ) -> Optional [str ]:
282
273
# Using stack_info=True will add 'error.stack_trace' even
283
274
# if the type is not 'error', exc_info=True only gathers
284
275
# when there's an active exception.
0 commit comments