HawkEx.Audit (hawk_ex v0.1.0)

Copy Markdown View Source

Records and queries audit log entries.

HawkEx audit logs are append-only records for business events. The module can be used directly by the host application and is also used internally by the audit listener to record HawkEx lifecycle events.

Automatic recording

HawkEx records internal billing events when PubSub is configured. The supervised audit listener subscribes to HawkEx.Events and writes received events as audit log entries.

Manual recording

Use track/3 to record your own application events:

HawkEx.Audit.track(
  current_user,
  "project.deleted",
  project
)

Querying

HawkEx.Audit.recent(limit: 50)
HawkEx.Audit.for_account(account_id)

Summary

Functions

Returns all audit entries for a specific account id, newest first.

Returns a page of audit log entries, newest first.

Fast typeahead search across audit log actions.

Manually records an audit entry.

Functions

for_account(account_id)

Returns all audit entries for a specific account id, newest first.

This filters against resource_id, which is how HawkEx records account-level billing events.

recent(opts \\ [])

Returns a page of audit log entries, newest first.

Options

  • :page - 1-indexed page number. Defaults to 1.
  • :per_page - rows per page. Defaults to 50.
  • :search - optional search term matching action.

Returns a map with the page of entries plus pagination metadata, so the caller never needs a second query to know total pages.

Example

HawkEx.Audit.recent(page: 1, per_page: 50)
# => %{entries: [...], page: 1, per_page: 50, total_count: 312, total_pages: 7}

search(query, opts \\ [])

Fast typeahead search across audit log actions.

Accepts :limit in opts, defaulting to 5.

track(actor, action, resource \\ nil)

Manually records an audit entry.

actor can be any struct with an :id field, or nil for system actions. resource can be any struct with an :id field, or nil.

Example

HawkEx.Audit.track(current_user, "settings.updated", organization)