Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
madzak committed Feb 14, 2023
2 parents 7ad9717 + 4e9b241 commit 08d41b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/pythonjsonlogger/jsonlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import traceback
import importlib

from typing import Any, Dict, Union, List, Tuple
from typing import Any, Dict, Optional, Union, List, Tuple

from inspect import istraceback

Expand All @@ -25,7 +25,12 @@



def merge_record_extra(record: logging.LogRecord, target: Dict, reserved: Union[Dict, List], rename_fields: Dict[str,str]) -> Dict:
def merge_record_extra(
record: logging.LogRecord,
target: Dict,
reserved: Union[Dict, List],
rename_fields: Optional[Dict[str,str]] = None,
) -> Dict:
"""
Merges extra attributes from LogRecord object into target dictionary
Expand All @@ -35,6 +40,8 @@ def merge_record_extra(record: logging.LogRecord, target: Dict, reserved: Union[
:param rename_fields: an optional dict, used to rename field names in the output.
Rename levelname to log.level: {'levelname': 'log.level'}
"""
if rename_fields is None:
rename_fields = {}
for key, value in record.__dict__.items():
# this allows to have numeric keys
if (key not in reserved
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@ def test_rename_reserved_attrs(self):
msg = self.buffer.getvalue()
self.assertEqual(msg, '{"error.type": null, "error.message": null, "log.origin.function": "test_rename_reserved_attrs", "log.level": "INFO", "log.origin.file.name": "tests", "process.name": "MainProcess", "process.thread.name": "MainThread", "log.message": "message"}\n')

def test_merge_record_extra(self):
record = logging.LogRecord("name", level=1, pathname="", lineno=1, msg="Some message", args=None, exc_info=None)
output = jsonlogger.merge_record_extra(record, target=dict(foo="bar"), reserved=[])
self.assertIn("foo", output)
self.assertIn("msg", output)
self.assertEquals(output["foo"], "bar")
self.assertEquals(output["msg"], "Some message")


if __name__ == '__main__':
if len(sys.argv[1:]) > 0:
Expand Down

0 comments on commit 08d41b3

Please sign in to comment.