From f63f6e74c6b571b3a89827d283718e44859ba177 Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Fri, 7 Aug 2026 14:54:33 +0000 Subject: [PATCH] Include extra also on transaction abort messages. --- docs/database.md | 3 ++- kanta/logging.py | 7 +++++-- kanta/transaction.py | 1 + tests/test_logemit.py | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/database.md b/docs/database.md index 06c8e9f..96e9a3c 100644 --- a/docs/database.md +++ b/docs/database.md @@ -242,7 +242,8 @@ def resolve_user_key(value: str) -> str | None: emitters can reuse as-is or replace piecemeal: - `event.header` — a lazy property producing the default one-line header for any kind: `[ ][ by ]` for changes, - `[ by ] transaction aborted: ` for aborts, and the + `[ ][ by ] transaction aborted: ` for aborts, + and the plain `Created`/`Migrated` summaries. It is settable: assign `event.header = ...` and return truthy to restyle the header while keeping the default diff routing. diff --git a/kanta/logging.py b/kanta/logging.py index 18c4a08..094440d 100644 --- a/kanta/logging.py +++ b/kanta/logging.py @@ -74,8 +74,9 @@ class LogEvent(msgspec.Struct, kw_only=True): """The default one-line header for this event, built on first access. Covers every event kind: ``"[ ][ by ]"`` for - changes, ``"[ by ] transaction aborted: "`` for - aborts, and the plain ``Created``/``Migrated`` summaries. + changes, ``"[ ][ by ] transaction aborted: + "`` for aborts, and the plain ``Created``/``Migrated`` + summaries. """ if self._header is None: self._header = self._build_header() @@ -102,6 +103,8 @@ class LogEvent(msgspec.Struct, kw_only=True): if self.kind == "change": return format_action_header(self.action or "", self.user, self.extra) line = Line().action(self.action or "") + if self.extra: + line(" ").target(self.extra) if self.user: line(" by ").user(self.user) line(f" transaction aborted: {self.error}") diff --git a/kanta/transaction.py b/kanta/transaction.py index bb56008..0f44cae 100644 --- a/kanta/transaction.py +++ b/kanta/transaction.py @@ -124,6 +124,7 @@ def transaction( kanta=impl._kanta, action=action, user=resolved_user, + extra=extra, error=exc, ), impl.callback_registry.logemit_handlers, diff --git a/tests/test_logemit.py b/tests/test_logemit.py index 9bca8a5..7613627 100644 --- a/tests/test_logemit.py +++ b/tests/test_logemit.py @@ -282,12 +282,13 @@ async def test_aborted_transaction_includes_resolved_user( await kanta.open() with caplog.at_level(logging.WARNING, logger="kanta.transaction"): with pytest.raises(ValueError): - with kanta.transaction(action="reset", user="u1") as data: + with kanta.transaction(action="reset", user="u1", extra="exp") as data: data.counter = 99 raise ValueError("boom") await kanta.close() messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING] + assert any("exp" in m for m in messages) assert any(" by " in m and "Alice" in m for m in messages) assert any(" transaction aborted: boom" in m for m in messages)