Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/logfmter/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import traceback
from contextlib import closing
from types import TracebackType
from typing import Dict, List, Optional, Tuple, Type, cast
from typing import Any, Dict, List, Optional, Tuple, Type, cast

ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]

Expand Down Expand Up @@ -134,11 +134,28 @@ def get_extra(cls, record: logging.LogRecord) -> dict:
"""
Return a dictionary of logger extra parameters by filtering any reserved keys.
"""
return {
cls.normalize_key(key): value
for key, value in record.__dict__.items()
if key not in RESERVED
}
extras = dict()
for key, value in record.__dict__.items():
key = cls.normalize_key(key)
if key in RESERVED:
continue
if isinstance(value, dict):
extras.update(cls.flatten_dict(value, key))
else:
extras[key] = value
return extras

@classmethod
def flatten_dict(cls, d: dict, parent_key: str = "") -> dict[str, Any]:
items = []
for key, value in d.items():
key = cls.normalize_key(key)
new_key = f"{parent_key}.{key}" if parent_key else key
if isinstance(value, dict):
items.extend(cls.flatten_dict(value, new_key).items())
else:
items.append([new_key, value])
return dict(items)

def __init__(
self,
Expand All @@ -158,9 +175,7 @@ def format(self, record: logging.LogRecord) -> str:
record.asctime = self.formatTime(record, self.datefmt)

if isinstance(record.msg, dict):
params = {
self.normalize_key(key): value for key, value in record.msg.items()
}
params = self.flatten_dict(record.msg)
else:
params = {"msg": record.getMessage()}

Expand All @@ -175,7 +190,6 @@ def format(self, record: logging.LogRecord) -> str:
# available under a different attribute name, then the formatter's mapping will
# be used to lookup these attributes. e.g. 'at' from 'levelname'
for key in self.keys:

attribute = key

# If there is a mapping for this key's attribute, then use it to lookup
Expand Down
16 changes: 15 additions & 1 deletion tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,21 @@ def test_normalize_key(value, expected):

@pytest.mark.parametrize(
"record,expected",
[({"msg": "test"}, {}), ({"value": 1}, {"value": 1})],
[
# No extras
({"msg": "test"}, {}),
# Regular extra
({"value": 1}, {"value": 1}),
# Nested extras
({"foo": {"bar": "baz"}}, {"foo.bar": "baz"}),
# Regular and nested
({"value": 1, "foo": {"bar": "baz"}}, {"value": 1, "foo.bar": "baz"}),
# Multiple nested under one
(
{"foo": {"bar": "baz", "joe": "smith"}},
{"foo.bar": "baz", "foo.joe": "smith"},
),
],
)
def test_get_extra(record, expected):
# Generate a real `logging.LogRecord` from the provided dictionary.
Expand Down
Loading