Skip to content

Commit

Permalink
Make ProcessorFormatter able to log dict (#189)
Browse files Browse the repository at this point in the history
Closes #188,
  • Loading branch information
ZlatyChlapec authored and hynek committed Feb 2, 2019
1 parent 9ef7959 commit 8917d5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def format(self, record):
# Make a shallow copy of the record to let other handlers/formatters
# process the original one
record = logging.makeLogRecord(record.__dict__)
if isinstance(record.msg, dict):
try:
# Both attached by wrap_for_formatter
logger = record._logger
meth_name = record._name
Expand All @@ -460,7 +460,7 @@ def format(self, record):
# processed by multiple logging formatters. LogRecord.getMessage
# would transform our dict into a str.
ed = record.msg.copy()
else:
except AttributeError:
logger = None
meth_name = record.levelname.lower()
ed = {"event": record.getMessage(), "_record": record}
Expand Down
15 changes: 14 additions & 1 deletion tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def test_foreign_delegate(self, configure_for_pf, capsys):

assert ("", "foo [in test_foreign_delegate]\n") == capsys.readouterr()

def test_clears_args(self, capsys, configure_for_pf):
def test_clears_args(self, configure_for_pf, capsys):
"""
We render our log records before sending it back to logging. Therefore
we must clear `LogRecord.args` otherwise the user gets an
Expand All @@ -511,6 +511,19 @@ def test_clears_args(self, capsys, configure_for_pf):
"hello world. [in test_clears_args]\n",
) == capsys.readouterr()

def test_log_dict(self, configure_for_pf, capsys):
"""
Test that dicts can be logged with std library loggers.
"""
configure_logging(None)

logging.getLogger().warning({"foo": "bar"})

assert (
"",
"{'foo': 'bar'} [in test_log_dict]\n",
) == capsys.readouterr()

def test_foreign_pre_chain(self, configure_for_pf, capsys):
"""
If foreign_pre_chain is an iterable, it's used to pre-process
Expand Down

0 comments on commit 8917d5d

Please sign in to comment.