Final cleanup: lazy logfmt build, docstring and example fixes

- transaction.py only builds the logfmt chain when logging is enabled
- callbacks.py module docstring documents the logemit special case
- docs logemit example uses the settable header instead of a user lookup
  that would break when logfmt resolution is registered
This commit is contained in:
Leo Vasanko
2026-08-07 06:17:17 +00:00
parent ecd146f72c
commit 47f38f4a71
3 changed files with 8 additions and 4 deletions
+3 -3
View File
@@ -269,9 +269,9 @@ def resolve_user_key(value: str) -> str | None:
def emit(ev: LogEvent):
if ev.kind != "change":
return default_emit(ev) # delegate, no chaining needed
actor = ev.current.get("users", {}).get(ev.user, {}).get("name", ev.user)
line = Line().user(actor, width=20)(" ").action(ev.action)
ev.logger.log(ev.level, f"{line}\n" + "\n".join(ev.diff_lines))
# Restyle the header; default_emit keeps routing the diff body.
ev.header = str(Line().user(ev.user or "-", width=20)(" ").action(ev.action))
return True
```
#### Terminal Formatting Helpers
+4
View File
@@ -7,6 +7,10 @@ default value.
Log formatters are a special case: they are called per value being rendered
and receive the value plus an optional ``path`` string. They return
``str | None``; ``None`` means "fall through to the next formatter".
Log emitters (``logemit``) are another special case: plain callables that
receive a :class:`kanta.logging.LogEvent` and are dispatched by
:func:`kanta.logging.emit_event`.
"""
from __future__ import annotations
+1 -1
View File
@@ -90,8 +90,8 @@ def transaction(
previous = impl.statedict
record = impl.queue_change(action, new_dict, user=user, mtime=mtime)
if record is not None:
logfmt = _build_logfmt(impl, previous, new_dict)
if log is not False:
logfmt = _build_logfmt(impl, previous, new_dict)
logger = (
log if isinstance(log, logging.Logger) else transaction_logger
)