Skip to content
keypal
Esc
↑↓navigate↵open⌘Jpreview
On this page

Audit logs

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.

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

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

PropType
keyId?string

Only entries for this key.

Typestring
ownerId?string

Only entries for this owner.

Typestring
action?AuditAction

Only entries with this action.

TypeAuditAction
startDate?string

ISO date or timestamp; entries at or after it.

Typestring
endDate?string

ISO date or timestamp; entries at or before it.

Typestring
limit?number

Maximum entries returned by getLogs.

Typenumber
Default100
offset?number

Entries to skip, for paging getLogs.

Typenumber
Default0

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

getLogs()

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

Returns matching entries, newest first.

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

countLogs()

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

Counts all matching entries, ignoring limit and offset.

deleteLogs()

deleteLogs(query: AuditLogQuery): Promise<number>

Deletes matching entries and returns how many were deleted.

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

clearLogs()

clearLogs(keyId: string): Promise<number>

Same as deleteLogs({ keyId }).

getLogStats()

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

Was this page helpful?