|
24 | 24 | 'processName', 'relativeCreated', 'stack_info', 'thread', 'threadName') |
25 | 25 |
|
26 | 26 |
|
27 | | -def merge_record_extra(record: logging.LogRecord, target: Dict, reserved: Union[Dict, List]) -> Dict: |
| 27 | + |
| 28 | +def merge_record_extra(record: logging.LogRecord, target: Dict, reserved: Union[Dict, List], rename_fields: Dict[str,str]) -> Dict: |
28 | 29 | """ |
29 | 30 | Merges extra attributes from LogRecord object into target dictionary |
30 | 31 |
|
31 | 32 | :param record: logging.LogRecord |
32 | 33 | :param target: dict to update |
33 | 34 | :param reserved: dict or list with reserved keys to skip |
| 35 | + :param rename_fields: an optional dict, used to rename field names in the output. |
| 36 | + Rename levelname to log.level: {'levelname': 'log.level'} |
34 | 37 | """ |
35 | 38 | for key, value in record.__dict__.items(): |
36 | 39 | # this allows to have numeric keys |
37 | 40 | if (key not in reserved |
38 | 41 | and not (hasattr(key, "startswith") |
39 | 42 | and key.startswith('_'))): |
40 | | - target[key] = value |
| 43 | + target[rename_fields.get(key,key)] = value |
41 | 44 | return target |
42 | 45 |
|
43 | 46 |
|
@@ -172,7 +175,7 @@ def add_fields(self, log_record: Dict[str, Any], record: logging.LogRecord, mess |
172 | 175 |
|
173 | 176 | log_record.update(self.static_fields) |
174 | 177 | log_record.update(message_dict) |
175 | | - merge_record_extra(record, log_record, reserved=self._skip_fields) |
| 178 | + merge_record_extra(record, log_record, reserved=self._skip_fields, rename_fields=self.rename_fields) |
176 | 179 |
|
177 | 180 | if self.timestamp: |
178 | 181 | key = self.timestamp if type(self.timestamp) == str else 'timestamp' |
|
0 commit comments