The Audit Logging Engineer creates tamper-evident records of who did what and when. Audit logs are different from application logs: they record business-significant events (user deleted an account, admin changed a permission, invoice was modified) rather than system events (HTTP request received, cache miss). Audit logs must be immutable - append-only, never updated or deleted - and must include enough context to reconstruct exactly what happened and why.
Event Schema (Actor-Action-Resource Model)
// Every audit event has: WHO did WHAT to WHICH resource, WHEN, and from WHERE
interface AuditEvent {
id: string; // Unique, random UUID
timestamp: string; // ISO 8601, UTC
// Actor: who performed the action
actor: {
type: 'user' | 'api_key' | 'service' | 'system';
id: string;
email?: string; // Denormalised - user may be deleted later
ipAddress?: string;
userAgent?: string;
};
// Action: what happened
action: string; // snake_case verb.noun: 'user.deleted', 'invoice.paid', 'permission.granted'
status: 'success' | 'failure';
errorMessage?: string; // If status is failure
// Resource: what was acted upon
resource: {
type: string; // 'user', 'invoice', 'api_key'
id: string;
name?: string; // Human-readable, denormalised
};
// Context
organizationId?: string;
requestId?: string; // Link to application log for correlation
metadata?: Record<string, unknown>; // Before/after values, changed fields
}
// Write to an append-only table - no UPDATE or DELETE permission on audit_log
async function writeAuditLog(event: Omit<AuditEvent, 'id' | 'timestamp'>) {
await db.insert(auditLog).values({
id: crypto.randomUUID(),
timestamp: new Date().toISOString(),
...event,
metadata: event.metadata ? JSON.stringify(event.metadata) : null,
});
}
Denormalisation is Required
// Audit logs must be self-contained - the data they reference will change or be deleted
// Bad: store only user_id and look up name at query time
// Good: store user_id AND email/name at write time
await writeAuditLog({
actor: {
type: 'user',
id: req.user.id,
email: req.user.email, // Store now - user may be deleted later
ipAddress: req.ip,
},
action: 'user.deleted',
status: 'success',
resource: {
type: 'user',
id: targetUser.id,
name: targetUser.email, // Store now - record will be deleted
},
metadata: {
deletedBy: 'admin',
reason: req.body.reason,
},
});
Retention and Tamper Detection
- Keep audit logs for at least 1 year online, 7 years archived - SOC 2 requires 1 year, GDPR legal obligation can require 7 years for financial records
- Export logs to immutable storage (S3 Object Lock with Compliance mode) for long-term retention that even admins cannot delete
- Add a chain hash: each event's hash includes the previous event's hash - any modification breaks the chain and is detectable
- Expose audit log to enterprise customers via API and UI - a visible audit trail is a selling point for compliance-conscious buyers
- Never include secrets, passwords, or full credit card numbers in audit log metadata - logs are often accessible to support staff who shouldn't see these