Skip to content

Commit

Permalink
Fix possible KeyError using standard handler and "extra" dict (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jun 7, 2020
1 parent aee1068 commit 0940f92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
`Unreleased`_
=============

- Modify the way the ``extra`` dict is used by ``LogRecord`` in order to prevent possible ``KeyError`` with standard ``logging`` handlers (`#271 <https://github.com/Delgan/loguru/issues/271>`_).


`0.5.0`_ (2020-05-17)
=====================

Expand Down
2 changes: 1 addition & 1 deletion loguru/_simple_sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def write(self, message):
(),
(exc.type, exc.value, exc.traceback) if exc else None,
record["function"],
record["extra"],
{"extra": record["extra"]},
)
if exc:
record.exc_text = "\n"
Expand Down
13 changes: 11 additions & 2 deletions tests/test_standard_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@ def test_null_handler(capsys):

def test_extra_dict(capsys):
handler = StreamHandler(sys.stdout)
formatter = Formatter("[%(abc)s] %(message)s")
formatter = Formatter("%(extra)s %(message)s")
handler.setFormatter(formatter)
logger.add(handler, format="<{extra[abc]}> {message}", catch=False)
logger.bind(abc=123).info("Extra!")
out, err = capsys.readouterr()
assert out == "[123] <123> Extra!\n"
assert out == "{'abc': 123} <123> Extra!\n"
assert err == ""


def test_no_conflict_with_extra_dict(capsys):
handler = StreamHandler(sys.stdout)
logger.add(handler, format="{message}", catch=False)
logger.bind(args=True, name="foobar", message="Wut?").info("OK!")
out, err = capsys.readouterr()
assert out == "OK!\n"
assert err == ""


Expand Down

0 comments on commit 0940f92

Please sign in to comment.