Document transaction log headers: extra, logheader, toggles
This commit is contained in:
@@ -197,6 +197,40 @@ def resolve_user_key(value: str) -> str | None:
|
||||
return names_by_id.get(value)
|
||||
```
|
||||
|
||||
#### Transaction Log Headers
|
||||
|
||||
- By default a transaction is logged with an `action by user` header followed
|
||||
by the diff lines. Added paths are colored green, deleted paths red.
|
||||
- `kanta.transaction(..., extra=...)` accepts display-only metadata that is
|
||||
used for logging and is never persisted in the `ChangeRecord`:
|
||||
- a string is appended literally after the action in the default header,
|
||||
- a dict is passed to a registered `@kanta.logheader` callback; if it has
|
||||
no `"target"` key, the database filename is inserted as the target.
|
||||
- Register a `@kanta.logheader` callback to replace the entire header line.
|
||||
It may declare `action: str`, `user: str | None` and `extra: dict | None`
|
||||
parameters, and can also have `DictPre`/`DictPost` state dicts and the
|
||||
`Kanta` instance injected. It must be synchronous and return `str | None`.
|
||||
- Multiple logheader callbacks are stacked in registration order; the first
|
||||
callback to return a non-`None` result wins. If all return `None`, Kanta
|
||||
falls back to the default header. The `user` value passed to the callback
|
||||
has already been through the `logfmt` formatters.
|
||||
- The header and diff parts can be toggled independently per transaction:
|
||||
`kanta.transaction(..., log={"header": True, "diff": False})`.
|
||||
|
||||
```python
|
||||
@kanta.logheader
|
||||
def format_header(action: str, user: str | None, extra: dict | None) -> str:
|
||||
session = extra.get("session_id", "-")
|
||||
return f"{user:<20} {session:>2} {action} {extra['target']}"
|
||||
|
||||
with kanta.transaction(
|
||||
action="update",
|
||||
user="alice",
|
||||
extra={"session_id": 3, "target": "Project Name (abcd1234)"},
|
||||
) as data:
|
||||
...
|
||||
```
|
||||
|
||||
## Migrations
|
||||
|
||||
- Migration source is configured on `Kanta(...)` via `migrations=`.
|
||||
|
||||
Reference in New Issue
Block a user