Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Add requirement for ``TERM`` environment variable not to be ``"dumb"`` to enable colorization (`#1287 <https://github.com/Delgan/loguru/pull/1287>`_, thanks `@snosov1 <https://github.com/snosov1>`_).
- Make ``logger.catch()`` usable as an asynchronous context manager (`#1084 <https://github.com/Delgan/loguru/issues/1084>`_).
- Make ``logger.catch()`` compatible with asynchronous generators (`#1302 <https://github.com/Delgan/loguru/issues/1302>`_).
- Add ``record["template"]`` that includes the raw, unformatted message (`#1349 <https://github.com/Delgan/loguru/issues/1349>`_).


`0.7.3`_ (2024-12-06)
Expand Down
1 change: 1 addition & 0 deletions loguru/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Record(TypedDict):
module: str
name: Optional[str]
process: RecordProcess
template: str
thread: RecordThread
time: datetime

Expand Down
1 change: 1 addition & 0 deletions loguru/_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def _serialize_record(text, record):
"module": record["module"],
"name": record["name"],
"process": {"id": record["process"].id, "name": record["process"].name},
"template": record["template"],
"thread": {"id": record["thread"].id, "name": record["thread"].name},
"time": {"repr": record["time"], "timestamp": record["time"].timestamp()},
},
Expand Down
1 change: 1 addition & 0 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,7 @@ def _log(self, level, from_decorator, options, message, args, kwargs):
"module": splitext(file_name)[0],
"name": name,
"process": RecordProcess(process.ident, process.name),
"template": str(message),
"thread": RecordThread(thread.ident, thread.name),
"time": current_datetime,
}
Expand Down
25 changes: 25 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,31 @@ def format(record):
assert output == "[123]\n"


def test_template_vs_message_with_formatting(writer):
logger.add(writer, format="{template} | {message}")
logger.info("Hello {name}", name="World")

assert writer.read() == "Hello {name} | Hello World\n"


@pytest.mark.parametrize(
("message", "args", "kwargs", "expected_template", "expected_message"),
[
("{} + {} = {}", [1, 2, 3], {}, "{} + {} = {}", "1 + 2 = 3"),
("{a} + {b} = {c}", [], {"a": 1, "b": 2, "c": 3}, "{a} + {b} = {c}", "1 + 2 = 3"),
("{0} + {two} = {1}", [1, 3], {"two": 2}, "{0} + {two} = {1}", "1 + 2 = 3"),
("{:.2f}", [1], {}, "{:.2f}", "1.00"),
],
)
def test_template_preserves_unformatted_message(
writer, message, args, kwargs, expected_template, expected_message
):
logger.add(writer, format="{template} | {message}", colorize=False)
logger.info(message, *args, **kwargs)

assert writer.read() == "{} | {}\n".format(expected_template, expected_message)


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