Prompt
How do I add audit logs to a SaaS app for compliance?
Latest observation
To add audit logs to a SaaS app for compliance, design them as a tamper-evident, append-only record of security- and data-relevant actions. The goal is to answer: who did what, to which object, when, from where, and what changed.
1) Define what must be logged
Start with compliance and risk requirements (SOC 2, ISO 27001, HIPAA, GDPR, SOX, etc.). Common events:
- Authentication
- login success/failure
- MFA enrollment/disable
- password reset
- session creation/revocation
- Authorization / admin actions
- role changes
- permission changes
- tenant/org settings changes
- API key creation/revocation
- Data access
- view/export/download sensitive records
- bulk reads
- report generation
- Data modification
- create/update/delete of important objects
- before/after values for critical fields
- Security operations
- integration changes
- webhook changes
- key/certificate rotation
- retention policy changes
- Compliance-sensitive actions
- consent changes
- retention/deletion requests
- legal hold actions
2) Use a consistent event schema
Store logs in a structured format, not plain text.
Typical fields:
event_idtimestamp(UTC)actor_type(user/service/admin)actor_idtenant_idaction(e.g.user.role.updated)resource_typeresource_idresult(success/failure)ip_addressuser_agentrequest_id/trace_idold_value/new_valuefor changed fieldsmetadata(free-form, but controlled)performed_byif acting on behalf of another usersource(UI/API/background job)
Example:
{
"event_id": "evt_123",
"timestamp": "2026-10-23T12:34:56Z",
"tenant_id": "tenant_001",
"actor_type": "user",
"actor_id": "user_42",
"action": "role.updated",
"resource_type": "member",
"resource_id": "user_42",
"result": "success",
"ip_address": "203.0.113.10",
"request_id": "req_abc",
"old_value": {"role": "member"},
"new_value": {"role": "admin"}
}
3) Make logs tamper-evident
Compliance usually expects logs to be protected from alteration.
Best practices:
- Append-only storage
- Restrict write/delete permissions
- Separate log storage from app DB
- Encrypt at rest and in transit
- Use hash chaining or periodic signed digests
- Consider WORM storage / immutable buckets where appropriate
- Strict admin access controls and MFA for log access
A simple pattern:
- each event includes
prev_event_hash - compute
event_hashover the event payload - periodically store signed checkpoints
4) Capture logs at the right layer
Log at:
- Application layer for business actions and meaningful context
- Auth layer for login/session/MFA
- Data access layer for sensitive reads/exports
- Admin/service layer for background and privileged changes
Avoid relying only on web server access logs—they’re useful, but not sufficient for compliance.
5) Handle sensitive data carefully
Audit logs can become a privacy risk if they contain secrets or personal data.
Do:
- log IDs, not full records
- mask sensitive fields
- avoid passwords, tokens, credit card data, PHI, secrets
- use allowlists for metadata
- define retention and deletion rules
Example:
- log “SSN changed” rather than the SSN itself
- log last 4 digits only if necessary and permitted
6) Keep logs searchable and reportable
You’ll need to answer audit questions quickly.
Add:
- tenant filtering
- time-range search
- actor/resource/action filters
- export to CSV/JSON
- immutable admin audit viewer
- retention policy per compliance need
Common integrations:
- SIEM: Splunk, Datadog, Elastic, Sentinel, etc.
- Event pipeline: Kafka, Kinesis, Pub/Sub
- Storage: S3/Object Lock, BigQuery, Snowflake, Postgres for smaller scale
7) Secure retention and access
Define:
- retention duration by event type
- archival policy
- deletion policy for expired logs
- break-glass access procedure
- who can view/export logs
Typical retention:
- 90 days hot/searchable
- 1–7 years archived depending on requirements
8) Build in detection and alerts
Audit logs are for both compliance and security monitoring.
Examples:
- admin role granted
- MFA disabled
- many failed logins
- bulk exports
- API key created outside business hours
- settings changes by unusual actor
9) Test completeness
Verify that:
- every sensitive action generates exactly one event
- failures are logged too
- logs are written even when the business transaction fails or partially fails
- clock/timezone is consistent
- tenant boundaries are correct
- logs are not duplicated or lost under retries
10) Practical implementation pattern
A common architecture:
- User performs action
- App executes business logic
- Audit event is created in code
- Event is sent to an async queue or logging service
- Consumer writes to immutable storage / SIEM
- Periodic integrity checks and alerts
For critical actions, consider a transactional outbox pattern so the business write and audit event are reliably committed.
11) Minimum compliance checklist
- Structured events
- UTC timestamps
- Actor, action, resource, result
- Tenant isolation
- Append-only / tamper-evident storage
- Restricted access
- Sensitive data redaction
- Retention policy
- Search/export capability
- Alerts for high-risk actions
- Tested coverage of critical flows
If you want, I can also give you:
- a sample audit log schema for a multi-tenant SaaS,
- a database design for storing audit logs,
- or code examples in a specific stack like Node.js, Python, or Rails.