Skip to content

Commit

Permalink
Fix error of "serialize" with "catch()" / "opt(record=True)" (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jun 12, 2020
1 parent b8e9575 commit 4c0e715
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- 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>`_).
- Add a new ``default`` optional argument to ``logger.catch()``, it should be the returned value by the decorated function in case an error occurred (`#272 <https://github.com/Delgan/loguru/issues/272>`_).
- Fix ``ValueError`` when using ``serialize=True`` in combination with ``logger.catch()`` or ``logger.opt(record=True)`` due to circular reference of the ``record`` dict (`#286 <https://github.com/Delgan/loguru/issues/286>`_).


`0.5.0`_ (2020-05-17)
Expand Down
6 changes: 3 additions & 3 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,9 @@ def _log(self, level_id, static_level_no, from_decorator, options, message, args
args = [arg() for arg in args]
kwargs = {key: value() for key, value in kwargs.items()}

if capture and kwargs:
log_record["extra"].update(kwargs)

if record:
if "record" in kwargs:
raise TypeError(
Expand All @@ -1934,9 +1937,6 @@ def _log(self, level_id, static_level_no, from_decorator, options, message, args
)
kwargs.update(record=log_record)

if capture and kwargs:
log_record["extra"].update(kwargs)

if colors:
if args or kwargs:
colored_message = Colorizer.prepare_message(message, args, kwargs)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_add_option_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ def test_serialize_with_exception():
assert bool(sink.json["record"]["exception"])


def test_serialize_with_catch_decorator():
sink = JsonSink()
logger.add(sink, format="{message}", serialize=True, catch=False)

@logger.catch
def foo():
1 / 0

foo()

lines = sink.json["text"].splitlines()
assert lines[0].startswith("An error has been caught")
assert lines[-1] == "ZeroDivisionError: division by zero"
assert bool(sink.json["record"]["exception"])


def test_serialize_with_record_option():
sink = JsonSink()
logger.add(sink, format="{message}", serialize=True, catch=False)

logger.opt(record=True).info("Test", foo=123)

assert sink.json["text"] == "Test\n"
assert sink.dict["extra"] == {"foo": 123}


def test_serialize_not_serializable():
sink = JsonSink()
logger.add(sink, format="{message}", catch=False, serialize=True)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,35 @@ def test_record_in_kwargs_too(writer):
logger.opt(record=True).info("Foo {record}", record=123)


def test_record_not_in_extra():
extra = None

def sink(message):
nonlocal extra
extra = message.record["extra"]

logger.add(sink, catch=False)

logger.opt(record=True).info("Test")

assert extra == {}


def test_kwargs_in_extra_of_record():
message = None

def sink(message_):
nonlocal message
message = message_

logger.add(sink, format="{message}", catch=False)

logger.opt(record=True).info("Test {record[extra][foo]}", foo=123)

assert message == "Test 123\n"
assert message.record["extra"] == {"foo": 123}


def test_exception_boolean(writer):
logger.add(writer, format="{level.name}: {message}")

Expand Down

0 comments on commit 4c0e715

Please sign in to comment.