---
title: Audit logs
description: Query, count, delete and summarize audit log entries
---

These methods need `auditLogs: true` and a storage with audit-log methods. Memory and Redis storage have them; the Drizzle and Kysely adapters have them when given `auditLogTable`, and Prisma when given `auditLogModel`. For setup, see [Audit logging](/docs/concepts/audit-logging).

**Every method throws**:

- `AUDIT_LOGGING_DISABLED` when `auditLogs` is not `true`.
- `STORAGE_NOT_SUPPORTED` when the storage lacks the method (for example a `KeyLookup`, or an adapter without an audit log table).

## AuditLog

```typescript
type AuditLog = {
  id: string
  action: 'created' | 'revoked' | 'rotated' | 'enabled' | 'disabled'
  keyId: string
  ownerId: string
  timestamp: string                 // ISO timestamp
  data?: Record<string, unknown>    // the merged ActionContext
}
```

`data` holds `auditContext` merged with the context passed to the method, with their `metadata` objects merged. A `created` entry's metadata also includes the key's `name` and `scopes`; a `rotated` entry's includes `rotatedTo`. A failed log write is logged to the console and doesn't fail the action.

## AuditLogQuery

| Prop | Type | Default | Description |
| - | - | - | - |
| `keyId?` | `string` | - | Only entries for this key. |
| `ownerId?` | `string` | - | Only entries for this owner. |
| `action?` | `AuditAction` | - | Only entries with this action. |
| `startDate?` | `string` | - | ISO date or timestamp; entries at or after it. |
| `endDate?` | `string` | - | ISO date or timestamp; entries at or before it. |
| `limit?` | `number` | `100` | Maximum entries returned by getLogs. |
| `offset?` | `number` | `0` | Entries to skip, for paging getLogs. |

Filters combine with AND. `limit` and `offset` apply only to `getLogs`.

## getLogs()

```typescript
getLogs(query?: AuditLogQuery): Promise<AuditLog[]>
```

Returns matching entries, newest first.

```typescript
const page = await keys.getLogs({ ownerId: 'user_123', limit: 20, offset: 20 })
```

## countLogs()

```typescript
countLogs(query?: AuditLogQuery): Promise<number>
```

Counts all matching entries, ignoring `limit` and `offset`.

## deleteLogs()

```typescript
deleteLogs(query: AuditLogQuery): Promise<number>
```

Deletes matching entries and returns how many were deleted.

:::warning
A query with no `keyId`, `ownerId`, `action`, `startDate` or `endDate` deletes nothing and returns `0`, so a missing filter can't wipe the log.
:::

```typescript
const deleted = await keys.deleteLogs({ endDate: '2025-01-01' })
```

## clearLogs()

```typescript
clearLogs(keyId: string): Promise<number>
```

Same as `deleteLogs({ keyId })`.

## getLogStats()

```typescript
getLogStats(ownerId: string): Promise<AuditLogStats>
```

```typescript
type AuditLogStats = {
  total: number
  byAction: Partial<Record<AuditAction, number>>   // actions with no entries are absent
  lastActivity: string | null                      // newest entry's timestamp
}
```
