BBackend CourseLeroy Labs
0/0
Search the course

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.

● advancedHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

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

Hostel-Web-Appmodule/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.

Report-Manager-2@Auditable · AuditAspect · AuditListener · AuditPublisher · audit_logs · 24-month purge · AuditIntegrationTest

Declarative before and after capture (logBefore, logAfter, entityClass, idParameterName), with audit writes going through events and a listener. Retention is 24 months.

IR3D (Kontentt)@Audited (70) · @ActivityLogged (48) · ActivityLoggingAspect · ActivityLogEntry · RestrictedAccessRecorder

Two separate trails:

  • @Audited for 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.

SperoTek (in dev)audit/api/AuditRecorder · AuditEntry · audit_logs (append-only trigger) · SecurityAlerts · ComplianceReports · AuditLogController

An 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
  • 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).

Check yourself

1. Which is NOT typically in an audit record?
2. Why does IR3D log 403s?