audit_trail.events¶
Audit events, severities and the event registry.
RESERVED_PREFIXES
module-attribute
¶
Verb prefixes reserved for the library's built-in events.
Severity ¶
Bases: IntEnum
Default severity levels, used when the host does not configure its own.
AuditEvent ¶
Bases: str, Enum
Base class for audit events. It has no members, so it can be subclassed.
Each member is a verb carrying its metadata, built with event:
class ShopEvent(AuditEvent):
ORDER_PLACED = event("shop.order_placed", MySeverity.LOW, OrderPlaced)
REFUND_DENIED = event("shop.refund_denied", MySeverity.HIGH, durable=True)
Members compare equal to their verb string, and str() returns the verb.
Type checkers reject calling the class with a verb (ShopEvent("..."))
because of the custom __new__; look verbs up with
EventRegistry.get instead.
Attributes:
| Name | Type | Description |
|---|---|---|
severity |
IntEnum | None
|
Member of the configured severity enum. |
schema |
type[BaseModel] | None
|
Pydantic model that validates the payload, or |
durable |
bool
|
Written on a separate connection and committed immediately, whatever happens to the caller's transaction. |
fail_closed |
bool
|
The entry is committed before data is returned; a write
failure always raises. Implies |
Crud ¶
Bases: AuditEvent
Entity changes recorded by the session listener.
Their severity depends on the model (AuditOptions), so it is resolved
by EventRegistry.severity_of rather than fixed here.
AuditSystem ¶
Bases: AuditEvent
Operations of the library itself.
Their severity is the registry's system_severity.
EventRegistryError ¶
Bases: ValueError
An event definition or a severity setting is invalid.
UnknownEventError ¶
Bases: LookupError
An event or verb is not registered.
PayloadError ¶
Bases: ValueError
A payload does not match its event's schema.
WriteFlags ¶
Bases: NamedTuple
Effective write mode of one entry.
Attributes:
| Name | Type | Description |
|---|---|---|
durable |
bool
|
Write on a separate connection and commit immediately. |
fail_closed |
bool
|
Raise on a write failure, before data is returned. |
EventRegistry ¶
EventRegistry(
severities: type[IntEnum],
*,
events: Iterable[type[AuditEvent]] | None = None,
default_severity: IntEnum | None = None,
system_severity: IntEnum | None = None,
)
All known events, validated against the configured severity enum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
severities
|
type[IntEnum]
|
The host's severity |
required |
events
|
Iterable[type[AuditEvent]] | None
|
Event classes to register. |
None
|
default_severity
|
IntEnum | None
|
Severity of |
None
|
system_severity
|
IntEnum | None
|
Severity of the library's |
None
|
Raises:
| Type | Description |
|---|---|
EventRegistryError
|
|
Source code in audit_trail/events.py
get ¶
Return the event registered for verb.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verb
|
str
|
The event's value, e.g. |
required |
Returns:
| Type | Description |
|---|---|
AuditEvent
|
The registered event. |
Raises:
| Type | Description |
|---|---|
UnknownEventError
|
|
Source code in audit_trail/events.py
severity_of ¶
Return the severity of an event or verb.
Crud events use options.verb_severity[verb], then
options.severity, then default_severity. Library events without
a fixed severity use system_severity. options is ignored for
every other event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AuditEvent | str
|
A registered event or its verb. |
required |
options
|
AuditOptions | None
|
The audited model's options, for |
None
|
Returns:
| Type | Description |
|---|---|
IntEnum
|
A member of |
Raises:
| Type | Description |
|---|---|
UnknownEventError
|
The event is not registered. |
EventRegistryError
|
An |
Source code in audit_trail/events.py
event ¶
event(
value: str,
severity: IntEnum | None,
schema: type[BaseModel] | None = None,
*,
durable: bool = False,
fail_closed: bool = False,
) -> tuple[
str, IntEnum | None, type[BaseModel] | None, bool, bool
]
Build an AuditEvent member value with the flags named.
Enum passes a member's value to __new__ positionally, so this helper
exists to keep member definitions readable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
The verb, |
required |
severity
|
IntEnum | None
|
Member of the configured severity enum. |
required |
schema
|
type[BaseModel] | None
|
Pydantic model that validates the payload. |
None
|
durable
|
bool
|
Write on a separate connection and commit immediately. |
False
|
fail_closed
|
bool
|
Commit the entry before data is returned and raise on a
write failure. Implies |
False
|
Returns:
| Type | Description |
|---|---|
tuple[str, IntEnum | None, type[BaseModel] | None, bool, bool]
|
The member value: the positional arguments of |
Source code in audit_trail/events.py
validate_payload ¶
validate_payload(
event: AuditEvent,
payload: Mapping[str, object] | BaseModel | None,
) -> dict[str, object] | BaseModel | None
Check a payload against its event's schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AuditEvent
|
The event being logged. |
required |
payload
|
Mapping[str, object] | BaseModel | None
|
A mapping, an instance of |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object] | BaseModel | None
|
Without a schema, a copy of the mapping (or |
dict[str, object] | BaseModel | None
|
the validated model instance. |
Raises:
| Type | Description |
|---|---|
PayloadError
|
The payload does not match the schema, is missing while a schema is declared, or is not a mapping while none is. |
Source code in audit_trail/events.py
resolve_write_flags ¶
Combine an event's write mode with a per-call durable override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
AuditEvent
|
The event being logged. |
required |
durable
|
bool | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
WriteFlags
|
The effective flags. |
Raises:
| Type | Description |
|---|---|
ValueError
|
|