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_DISABLEDwhenauditLogsis nottrue.STORAGE_NOT_SUPPORTEDwhen the storage lacks the method (for example aKeyLookup, 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
keyId?string
Only entries for this key.
stringownerId?string
Only entries for this owner.
stringaction?AuditAction
Only entries with this action.
AuditActionstartDate?string
ISO date or timestamp; entries at or after it.
stringendDate?string
ISO date or timestamp; entries at or before it.
stringlimit?number
Maximum entries returned by getLogs.
number100offset?number
Entries to skip, for paging getLogs.
number0Filters 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
}