# `PhoenixKit.AuditLog`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.165/lib/phoenix_kit/audit_log.ex#L1)

Context for managing audit logs in PhoenixKit.

Provides functionality for logging administrative actions such as password resets,
user modifications, and other sensitive operations that require tracking.

## Examples

    # Log an admin password reset
    PhoenixKit.AuditLog.log_password_change(%{
      target_user_uuid: 123,
      admin_user_uuid: 1,
      action: :admin_password_reset,
      ip_address: "192.168.1.1",
      user_agent: "Mozilla/5.0..."
    })

    # Query audit logs for a specific user
    PhoenixKit.AuditLog.list_logs_for_user(123)

    # Query audit logs by action type
    PhoenixKit.AuditLog.list_logs_by_action(:admin_password_reset)

# `count_logs`

Counts audit log entries with optional filters.

## Options
  Same options as `list_logs/1` except `:limit` and `:offset`

## Examples

    iex> count_logs(action: :admin_password_reset)
    42

# `create_log_entry`

Creates a generic audit log entry.

## Parameters
  * `attrs` - Map containing:
    * `:target_user_uuid` - ID of the user affected by the action (required)
    * `:admin_user_uuid` - ID of the admin who performed the action (required)
    * `:action` - The action performed (required)
    * `:ip_address` - IP address of the admin (optional)
    * `:user_agent` - User agent string of the admin (optional)
    * `:metadata` - Additional metadata (optional)

## Examples

    iex> create_log_entry(%{
    ...>   target_user_uuid: 123,
    ...>   admin_user_uuid: 1,
    ...>   action: :user_created,
    ...>   ip_address: "192.168.1.1"
    ...> })
    {:ok, %Entry{}}

# `get_log!`

Gets a single audit log entry by ID.

## Examples

    iex> get_log!(123)
    %Entry{}

    iex> get_log!(456)
    ** (Ecto.NoResultsError)

# `list_logs`

Lists all audit log entries with optional filters.

## Options
  * `:limit` - Maximum number of entries to return (default: 100)
  * `:offset` - Number of entries to skip (default: 0)
  * `:action` - Filter by action type
  * `:target_user_uuid` - Filter by target user ID
  * `:admin_user_uuid` - Filter by admin user ID
  * `:from_date` - Filter entries after this date
  * `:to_date` - Filter entries before this date

## Examples

    iex> list_logs(limit: 50, action: :admin_password_reset)
    [%Entry{}, ...]

# `list_logs_by_action`

Lists all audit log entries by action type.

## Examples

    iex> list_logs_by_action(:admin_password_reset)
    [%Entry{}, ...]

# `list_logs_for_user`

Lists all audit log entries for a specific user.

Returns entries where the user is either the target or the admin.

## Examples

    iex> list_logs_for_user(123)
    [%Entry{}, ...]

# `log_password_change`

Logs a password change action performed by an admin.

## Parameters
  * `attrs` - Map containing:
    * `:target_user_uuid` - ID of the user whose password was changed (required)
    * `:admin_user_uuid` - ID of the admin who performed the action (required)
    * `:action` - The action performed (default: `:admin_password_reset`)
    * `:ip_address` - IP address of the admin (optional)
    * `:user_agent` - User agent string of the admin (optional)
    * `:metadata` - Additional metadata (optional)

## Examples

    iex> log_password_change(%{
    ...>   target_user_uuid: 123,
    ...>   admin_user_uuid: 1,
    ...>   action: :admin_password_reset,
    ...>   ip_address: "192.168.1.1"
    ...> })
    {:ok, %Entry{}}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
