Skip to content

Commit 0d011f2

Browse files
committed
Add template to record
1 parent 66d6de1 commit 0d011f2

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- 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>`_).
1313
- Make ``logger.catch()`` usable as an asynchronous context manager (`#1084 <https://github.com/Delgan/loguru/issues/1084>`_).
1414
- Make ``logger.catch()`` compatible with asynchronous generators (`#1302 <https://github.com/Delgan/loguru/issues/1302>`_).
15+
- Add ``record["template"]`` that includes the raw, unformatted message (`#1349 https://github.com/Delgan/loguru/issues/1349`_).
1516

1617

1718
`0.7.3`_ (2024-12-06)

loguru/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Record(TypedDict):
105105
module: str
106106
name: Optional[str]
107107
process: RecordProcess
108+
template: str
108109
thread: RecordThread
109110
time: datetime
110111

loguru/_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ def _serialize_record(text, record):
280280
"module": record["module"],
281281
"name": record["name"],
282282
"process": {"id": record["process"].id, "name": record["process"].name},
283+
"template": record["template"],
283284
"thread": {"id": record["thread"].id, "name": record["thread"].name},
284285
"time": {"repr": record["time"], "timestamp": record["time"].timestamp()},
285286
},

loguru/_logger.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,7 @@ def _log(self, level, from_decorator, options, message, args, kwargs):
21222122
"module": splitext(file_name)[0],
21232123
"name": name,
21242124
"process": RecordProcess(process.ident, process.name),
2125+
"template": str(message),
21252126
"thread": RecordThread(thread.ident, thread.name),
21262127
"time": current_datetime,
21272128
}

tests/test_formatting.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,31 @@ def format(record):
195195
assert output == "[123]\n"
196196

197197

198+
def test_template_vs_message_with_formatting(writer):
199+
logger.add(writer, format="{template} | {message}")
200+
logger.info("Hello {name}", name="World")
201+
202+
assert writer.read() == "Hello {name} | Hello World\n"
203+
204+
205+
@pytest.mark.parametrize(
206+
("message", "args", "kwargs", "expected_template", "expected_message"),
207+
[
208+
("{} + {} = {}", [1, 2, 3], {}, "{} + {} = {}", "1 + 2 = 3"),
209+
("{a} + {b} = {c}", [], {"a": 1, "b": 2, "c": 3}, "{a} + {b} = {c}", "1 + 2 = 3"),
210+
("{0} + {two} = {1}", [1, 3], {"two": 2}, "{0} + {two} = {1}", "1 + 2 = 3"),
211+
("{:.2f}", [1], {}, "{:.2f}", "1.00"),
212+
],
213+
)
214+
def test_template_preserves_unformatted_message(
215+
writer, message, args, kwargs, expected_template, expected_message
216+
):
217+
logger.add(writer, format="{template} | {message}", colorize=False)
218+
logger.info(message, *args, **kwargs)
219+
220+
assert writer.read() == f"{expected_template} | {expected_message}\n"
221+
222+
198223
@pytest.mark.parametrize("colors", [True, False])
199224
def test_missing_positional_field_during_formatting(writer, colors):
200225
logger.add(writer)

0 commit comments

Comments
 (0)