Audit logs & activity trails
What to record (who, what, when, where, before and after, why), audit vs activity vs application logs, capturing changes with AOP or explicit recorders, retention, immutability and compliance reports.
Three different “logs”
| Type | Audience | Example | Storage |
|---|---|---|---|
| Application log | Developers | ERROR NullPointerException in ... |
Files, CloudWatch, Loki (days to weeks) |
| Activity log | Users and admins | “You signed out of session on Chrome/Windows” | DB, shown in UI |
| Audit log | Auditors, security, compliance | “user X changed role of Y from VIEWER to ADMIN at 10:02 from IP Z, reason: promotion” | DB, immutable, months to years |
Anatomy of an audit record
{
"id": "0193...", "occurredAt": "2026-11-25T10:02:11Z",
"actor": { "type": "USER", "id": "u-17", "role": "OWNER", "ip": "41.x.x.x", "userAgent": "..." },
"companyId": "c-9",
"action": "journal.posted",
"target": { "type": "JournalEntry", "id": "je-42" },
"changes": { "status": ["SUBMITTED", "POSTED"] },
"reason": "month-end close",
"correlationId": "7d1c..."
}
In your projects
module/audit · @Audited · AuditAspect · AuditContext · AuditLog (changelog 003)An AOP @Audited(action, targetType) records the actor, role, action, target, old and new snapshots and the result. The service puts the “before” state into AuditContext.
@Auditable · AuditAspect · AuditListener · AuditPublisher · audit_logs · 24-month purge · AuditIntegrationTestDeclarative before and after capture (logBefore, logAfter, entityClass, idParameterName), with audit writes going through events and a listener. Retention is 24 months.
@Audited (70) · @ActivityLogged (48) · ActivityLoggingAspect · ActivityLogEntry · RestrictedAccessRecorderTwo separate trails:
@Auditedfor admin and security actions.@ActivityLogged(action = "SESSION_REVOKED", targetType = "RefreshToken", detail = "Signed out of session {0}")for user-facing activity, with message templates.
RestrictedAccessRecorder logs 403s, because denied attempts are security signals.
audit/api/AuditRecorder · AuditEntry · audit_logs (append-only trigger) · SecurityAlerts · ComplianceReports · AuditLogControllerAn explicit fluent recorder instead of AOP:
audit.record(AuditEntry.of("journal.posted")
.forCompany(companyId).on("JournalEntry", entryId)
.changing("status", "SUBMITTED", "POSTED")
.because(reason));audit_logs is append-only through a trigger (AuditLogAppendOnlyIT). On top of it sit compliance reports (access review, login history, permission changes) and SecurityAlerts.
Alternatives
| Tool | Notes |
|---|---|
| Custom table (yours) | Full control |
| Hibernate Envers | Automatic revision tables for every entity change |
| Debezium CDC | Captures every DB change from the WAL, including raw SQL |
Postgres pgaudit |
Statement-level DB audit |
| Ship to a SIEM (Datadog, Splunk, Elastic) | Security analytics and alerting |
Related concepts you must know
- Don’t put secrets or PII in audit details. Store ids, not passwords or full card numbers.
- Tamper evidence. Hash-chain the records (each row stores the hash of the previous one), or write to WORM storage (S3 Object Lock).
- Correlation ids link an audit row to the request’s application logs.
- Retention vs GDPR. You may need to pseudonymise actors after a user deletes their account.
- Index for the questions you’ll ask: (company_id, occurred_at), (target_type, target_id), (actor_id).