1. Audit Log Paradigms
Core Concepts
Audit logging is the practice of recording changes to data in a system to provide:
- Traceability: Who did what, when, and why
- Compliance: Meeting regulatory requirements (GDPR, HIPAA, SOC 2, PCI DSS)
- Security: Detecting suspicious activities and unauthorized access
- Debugging: Understanding system behavior and data evolution
- Recovery: Ability to rollback or replay changes
Key Principles
1. Append-Only Structure
- Audit logs should NEVER be modified or deleted
- All entries are immutable after creation
- Use separate tables/storage from operational data
2. Essential Audit Fields
Every audit log entry should capture:
- Timestamp: When the action occurred (with timezone)
- Actor: Who performed the action (user ID, service account)
- Action: What was done (CREATE, UPDATE, DELETE, READ)
- Resource: What entity was affected (table name, record ID)
- Before/After State: Previous and new values
- Context: Additional metadata (IP address, session ID, request ID)
3. Performance Considerations
- Audit logging should not significantly impact main operations
- Use async/background processing for logging when possible
- Implement log rotation and archival strategies
- Index strategically for query performance
How This API Separates Audit Records from Revisions
The RefactorRoom API stores two related records for editorial changes. audit_logs records the actor, action, target, and timestamps. content_revisions stores each version's JSONB content and actor. content_entries points to the current revision and, when approved, to the approved revision.
That split answers two different questions: which operation happened, and what content version resulted from it? For example, an edit audit row identifies the operation and target. The associated revision row contains the versioned article data. The audit row is not a substitute for the revision itself.
This is one concrete audit design. The API's AuditLog model embeds Base, then adds Actor, Action, and Target. The migration defines the matching audit_logs columns.
References: GORM transactions.
Expanded from the supplied audit-log article
What is an audit log?
An audit log is a structured record of an action that matters to a system. Its purpose is to explain who did what, to which resource, when, and with what result. Depending on the business domain, it may also record the reason or request context.

An important business operation produces both the current state and an audit trail.
Audit logs help with several tasks:
- Traceability: Explain how a record reached its current state.
- Security investigation: Find unexpected edits, permission changes, or sensitive access.
- Operational debugging: Connect a user action to a request and database change.
- Accountability: Give reviewers a reliable sequence of actions.
- Compliance support: Provide evidence for specific controls when a law, contract, or standard requires it.
An audit log does not automatically make an application compliant with GDPR, HIPAA, SOC 2, or PCI DSS. Each framework has its own scope and requirements. It also does not automatically support rollback: that depends on what data you saved and how the application manages versions.
Audit logs are not normal application logs
A debug message such as updated article successfully is useful for a developer, but it may not contain a stable actor ID, target ID, or business action. It may disappear when ordinary application logs rotate.
For important changes, use a dedicated, queryable audit record rather than relying only on console logs.
The anatomy of an audit event
A good audit event should be clear enough that another engineer can understand it months later, without reading application source code.

Three useful groups of fields: identity, operation, and context.
A practical event might look like this:
{
"id": "audit_01",
"occurred_at": "2026-11-23T09:10:00Z",
"actor_id": "editor_42",
"actor_type": "user",
"action": "content.edit",
"resource_type": "content_entry",
"resource_id": "17",
"request_id": "req_abc123",
"outcome": "success",
"metadata": {
"new_revision_id": "203"
}
}This is an illustrative event, not a claim about the current RefactorRoom database schema.
| Field | Why it matters |
|---|---|
occurred_at |
Gives a sortable event time; store an unambiguous instant. |
actor_id and actor_type |
Identifies the user, service, or system that initiated the action. |
action |
Uses a stable business name, such as content.edit or content.approve. |
resource_type and resource_id |
Identifies the affected record. |
request_id |
Connects the audit event with application logs and traces. |
outcome |
Distinguishes a completed operation from a denied or failed attempt. |
metadata |
Holds a small, approved set of extra details. |
For many systems, a timestamp, actor, action, and target are the minimum useful fields. The exact set depends on the event.
Be careful with before/after values. They are useful when auditing small changes, but full snapshots can duplicate sensitive data and greatly increase storage. Sometimes a reference to an immutable revision is enough. Never copy passwords, authentication tokens, or secret request headers into audit metadata.
Series diagrams

Event sourcing replays ordered domain events to derive state. Audit logs record actions against current business records.
Further reading
Originally published on DEV Community.


