@@ -72,15 +72,16 @@ class StdlibFormatter(logging.Formatter):
72
72
converter = time .gmtime
73
73
74
74
def __init__ (
75
- self ,
76
- fmt = None ,
77
- datefmt = None ,
78
- style = "%" ,
79
- validate = None ,
80
- stack_trace_limit = None ,
81
- exclude_fields = (),
75
+ self , # type: Any
76
+ fmt = None , # type: Optional[str]
77
+ datefmt = None , # type: Optional[str]
78
+ style = "%" , # type: str
79
+ validate = None , # type: Optional[bool]
80
+ stack_trace_limit = None , # type: Optional[int]
81
+ extra = None , # type: Optional[Dict[str, Any]]
82
+ exclude_fields = (), # type: Sequence[str]
82
83
):
83
- # type: (Any, Optional[str], Optional[str], str, Optional[bool], Optional[int], Sequence[str] ) -> None
84
+ # type: (... ) -> None
84
85
"""Initialize the ECS formatter.
85
86
86
87
:param int stack_trace_limit:
@@ -89,6 +90,8 @@ def __init__(
89
90
Setting this to zero will suppress stack traces.
90
91
This setting doesn't affect ``LogRecord.stack_info`` because
91
92
this attribute is typically already pre-formatted.
93
+ :param Optional[Dict[str, Any]] extra:
94
+ Specifies the collection of meta-data fields to add to all records.
92
95
:param Sequence[str] exclude_fields:
93
96
Specifies any fields that should be suppressed from the resulting
94
97
fields, expressed with dot notation::
@@ -129,6 +132,7 @@ def __init__(
129
132
):
130
133
raise TypeError ("'exclude_fields' must be a sequence of strings" )
131
134
135
+ self ._extra = extra
132
136
self ._exclude_fields = frozenset (exclude_fields )
133
137
self ._stack_trace_limit = stack_trace_limit
134
138
@@ -218,6 +222,10 @@ def format_to_ecs(self, record):
218
222
# since they can be defined as 'extras={"http": {"method": "GET"}}'
219
223
extra_keys = set (available ).difference (self ._LOGRECORD_DICT )
220
224
extras = flatten_dict ({key : available [key ] for key in extra_keys })
225
+ # Merge in any global extra's
226
+ if self ._extra is not None :
227
+ for field , value in self ._extra .items ():
228
+ merge_dicts (de_dot (field , value ), extras )
221
229
222
230
# Pop all Elastic APM extras and add them
223
231
# to standard tracing ECS fields.
0 commit comments