Skip to content

Commit

Permalink
Fix possible exception while formatting non-string message (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Sep 20, 2020
1 parent b77f4fd commit 296635c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
`Unreleased`_
=============

- Fix possible exception during formatting of non-string messages (`#331 <https://github.com/Delgan/loguru/issues/331>`_).


`0.5.2`_ (2020-09-06)
=====================

Expand Down
4 changes: 2 additions & 2 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1914,7 +1914,7 @@ def _log(self, level_id, static_level_no, from_decorator, options, message, args
"function": code.co_name,
"level": RecordLevel(level_name, level_no, level_icon),
"line": frame.f_lineno,
"message": message,
"message": str(message),
"module": splitext(file_name)[0],
"name": name,
"process": RecordProcess(process.ident, process.name),
Expand All @@ -1941,7 +1941,7 @@ def _log(self, level_id, static_level_no, from_decorator, options, message, args
if args or kwargs:
colored_message = Colorizer.prepare_message(message, args, kwargs)
else:
colored_message = Colorizer.prepare_simple_message(message)
colored_message = Colorizer.prepare_simple_message(str(message))
log_record["message"] = colored_message.stripped
elif args or kwargs:
colored_message = None
Expand Down
58 changes: 58 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,64 @@ def sink(message):
]


def test_non_string_message(writer):
logger.add(writer, format="{message}")

logger.info(1)
logger.info({})
logger.info(b"test")

assert writer.read() == "1\n{}\nb'test'\n"


@pytest.mark.parametrize("colors", [True, False])
def test_non_string_message_is_str_in_record(writer, colors):
output = ""

def sink(message):
nonlocal output
assert isinstance(message.record["message"], str)
output += message

def format(record):
assert isinstance(record["message"], str)
return "[{message}]\n"

logger.add(sink, format=format, catch=False)
logger.opt(colors=colors).info(123)
assert output == "[123]\n"


@pytest.mark.parametrize("colors", [True, False])
def test_missing_positional_field_during_formatting(writer, colors):
logger.add(writer)

with pytest.raises(IndexError):
logger.opt(colors=colors).info("Foo {} {}", 123)


@pytest.mark.parametrize("colors", [True, False])
def test_missing_named_field_during_formatting(writer, colors):
logger.add(writer)

with pytest.raises(KeyError):
logger.opt(colors=colors).info("Foo {bar}", baz=123)


def test_not_formattable_message(writer):
logger.add(writer)

with pytest.raises(AttributeError):
logger.info(123, baz=456)


def test_not_formattable_message_with_colors(writer):
logger.add(writer)

with pytest.raises(TypeError):
logger.opt(colors=True).info(123, baz=456)


def test_invalid_color_markup(writer):
with pytest.raises(ValueError):
logger.add(writer, format="<red>Not closed tag", colorize=True)

0 comments on commit 296635c

Please sign in to comment.