Include extra also on transaction abort messages.

This commit is contained in:
Leo Vasanko
2026-08-07 14:54:33 +00:00
parent 47f38f4a71
commit f63f6e74c6
4 changed files with 10 additions and 4 deletions
+2 -1
View File
@@ -242,7 +242,8 @@ def resolve_user_key(value: str) -> str | None:
emitters can reuse as-is or replace piecemeal: emitters can reuse as-is or replace piecemeal:
- `event.header` — a lazy property producing the default one-line header - `event.header` — a lazy property producing the default one-line header
for any kind: `<action>[ <extra>][ by <user>]` for changes, for any kind: `<action>[ <extra>][ by <user>]` for changes,
`<action>[ by <user>] transaction aborted: <error>` for aborts, and the `<action>[ <extra>][ by <user>] transaction aborted: <error>` for aborts,
and the
plain `Created`/`Migrated` summaries. It is settable: assign plain `Created`/`Migrated` summaries. It is settable: assign
`event.header = ...` and return truthy to restyle the header while `event.header = ...` and return truthy to restyle the header while
keeping the default diff routing. keeping the default diff routing.
+5 -2
View File
@@ -74,8 +74,9 @@ class LogEvent(msgspec.Struct, kw_only=True):
"""The default one-line header for this event, built on first access. """The default one-line header for this event, built on first access.
Covers every event kind: ``"<action>[ <extra>][ by <user>]"`` for Covers every event kind: ``"<action>[ <extra>][ by <user>]"`` for
changes, ``"<action>[ by <user>] transaction aborted: <error>"`` for changes, ``"<action>[ <extra>][ by <user>] transaction aborted:
aborts, and the plain ``Created``/``Migrated`` summaries. <error>"`` for aborts, and the plain ``Created``/``Migrated``
summaries.
""" """
if self._header is None: if self._header is None:
self._header = self._build_header() self._header = self._build_header()
@@ -102,6 +103,8 @@ class LogEvent(msgspec.Struct, kw_only=True):
if self.kind == "change": if self.kind == "change":
return format_action_header(self.action or "", self.user, self.extra) return format_action_header(self.action or "", self.user, self.extra)
line = Line().action(self.action or "") line = Line().action(self.action or "")
if self.extra:
line(" ").target(self.extra)
if self.user: if self.user:
line(" by ").user(self.user) line(" by ").user(self.user)
line(f" transaction aborted: {self.error}") line(f" transaction aborted: {self.error}")
+1
View File
@@ -124,6 +124,7 @@ def transaction(
kanta=impl._kanta, kanta=impl._kanta,
action=action, action=action,
user=resolved_user, user=resolved_user,
extra=extra,
error=exc, error=exc,
), ),
impl.callback_registry.logemit_handlers, impl.callback_registry.logemit_handlers,
+2 -1
View File
@@ -282,12 +282,13 @@ async def test_aborted_transaction_includes_resolved_user(
await kanta.open() await kanta.open()
with caplog.at_level(logging.WARNING, logger="kanta.transaction"): with caplog.at_level(logging.WARNING, logger="kanta.transaction"):
with pytest.raises(ValueError): 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 data.counter = 99
raise ValueError("boom") raise ValueError("boom")
await kanta.close() await kanta.close()
messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING] 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(" by " in m and "Alice" in m for m in messages)
assert any(" transaction aborted: boom" in m for m in messages) assert any(" transaction aborted: boom" in m for m in messages)