Developer Implementation Guide
Maintain logging and audit trails
Capture the right events in a structured, centralized, and tamper-evident way. Minimize personal data in logs, restrict access, and keep enough history to investigate incidents and prove accountability.
Maintain Logging and Audit Trails
Capture the right events in a structured, centralized, and tamper-evident way. Minimize personal data in logs, restrict access, and keep enough history to investigate incidents and prove accountability.
Scope and Strategy
Define a logging policy: what to log, where to store it, who can access it, and how long to retain it.
Use structured JSON logs and a centralized sink or SIEM. Prefer append-only or object-lock storage.
Use UTC timestamps, synchronized time (NTP), and consistent schemas across services.
What to Log
Authentication: logins, MFA prompts, successes, failures, password resets.
Authorization: allow and deny decisions with role or permission evaluated.
Personal data access: reads, writes, exports, deletes, and bulk queries.
Admin and config: role changes, policy edits, feature flags, key and secret use.
Data lifecycle: retention changes, erasure requests, data subject request workflows.
Integrations: outbound transfers to vendors, webhook deliveries, and retries.
System health: deployment events, errors, rate limit triggers, anomaly detections.
Log Format and Fields
Use a consistent schema so queries and alerts work across services.
Correlation and Traceability
Generate and propagate a
request_idandtrace_idthrough gateways and services.Include them in every log line to reconstruct cross-service flows.
Privacy-Safe Logging
Do not log plaintext personal data, secrets, access tokens, or full card numbers.
Mask or tokenize values. Hash persistent identifiers with a secret salt for linkability without reversibility.
Enable log scrubbing in apps and ingestion pipelines. Block stack traces from dumping request bodies.
Storage, Integrity, and Access Control
Send logs to a dedicated project or account. Restrict with least privilege and break-glass access.
Enable immutability: object lock or WORM where supported. Keep a secondary copy in a separate region.
Make logs tamper-evident: chain records or sign batches. Record integrity metadata.
Detection and Alerting
Build rules for unusual patterns: spikes in 403 denials, MFA disabled, export volume jumps, decrypt spikes, logins from new countries, admin role grants.
Send high-severity alerts to on-call with runbooks and auto-ticket creation.
Retention and Deletion
Keep security and access logs long enough to investigate incidents, commonly 12 to 24 months, with legal hold support.
Separate short-lived verbose app logs from long-term audit records to control cost and exposure.
Document retention schedules and purge workflows. Verify deletion in both hot and archive stores.
Example: Postgres Row-Change Auditing
Capture who changed what without storing plaintext values.
Quick Logging and Audit Checklist
Use structured JSON logs with UTC timestamps and propagated request IDs
Centralize logs in a restricted, immutable store with multi-region copies
Log auth, data access, admin actions, exports, and lifecycle events
Avoid plaintext PII and secrets; hash or tokenize identifiers
Build alerts for high-risk patterns and maintain incident runbooks
Enforce retention schedules and verify purge and legal hold behavior
Make audit stores append-only and tamper-evident
Conclusion
Reliable logs and auditable trails turn surprises into timelines. With structured, privacy-safe logging, immutable storage, and clear retention policies, you can detect abuse quickly, investigate confidently, and meet GDPR and CPRA expectations for accountability and security.