Skip to content

Audit log

Introduction

An audit log is the "surveillance footage" of critical operations: logins, permission changes, deletions, exports, and transfers each leave a non-repudiable record.

How it differs from ordinary logs

Runtime logs (see Logging) Audit logs
Purpose Troubleshoot program issues Accountability, compliance, security forensics
Content Errors, performance, debug info Who did what to what
Retention Days to weeks Usually months to years, depending on regulation
Mutable? Does not matter Immutable, append-only

Why use it

  • Post-incident investigation: when data is tampered with or leaked, you can find the source.
  • Deterrence against insider abuse: knowing actions are recorded discourages misuse.
  • Compliance: industries that handle payments, healthcare, or personal data usually require a clear audit trail.
  • Dispute evidence: when a user says “I never changed that”, you have records.

How to do it

  1. Which events to record: successful/failed login, password change, permission changes (see RBAC), data create/update/delete, exports, admin actions, payments and refunds.
  2. Each record should at least include: actor, time, action, target (type and id), result (success/failure), source IP, request trace id; for updates you may also store before/after diffs.
  3. Append only; no update or delete: the account that writes the audit table gets INSERT only; for important cases, send to a separate log system or write-only storage.
  4. No sensitive plaintext: passwords, full card numbers, and tokens must not enter audit records—only the fact that “the password was changed”.
  5. Same fate as the business: put critical operations and the audit row in the same database transaction (see Transactions) so the business cannot succeed without a trail.
  6. At high volume, write asynchronously (via a message queue), but do not lose events.
  7. Review regularly: someone must look, and alerts (for example bulk exports outside working hours) make it meaningful.

Component comparison

Hand-written application-layer audit table

Hand-rolledOpen sourceOpsLow

In business code, when a key operation completes, insert a record into a dedicated "audit table" as a side step.

Best forProjects with few kinds of operations to record, and that need business semantics (login, export, permission changes).

Avoid whenYou need to automatically track every change to every table.

Pros

Most intuitive, and you decide what to record and in how much detail

Can record business meaning (such as "an admin exported the customer list"), not just data changes

Does not depend on a specific library or database feature

Cons

Relies entirely on developer discipline, and one missed spot means one missing record

Code must be added manually to every feature, and easily gets mixed into business logic

Operations that modify the database directly, bypassing the application, are not recorded

ORM interceptors / signals (EF Core SaveChanges interceptor, Django signals)

Built into the frameworkOpsLow

Intercept at the moment the framework saves data and write "what changed, before and after values" into the audit table uniformly, so business code does not need it everywhere.

Best forProjects that want automatic data change history without adding recording code at every business spot.

Avoid whenHeavy use of bulk updates or raw SQL, or when you also need to record business actions that are not data changes.

Pros

Centralized in one place, so ORM changes are hard to miss

It records the before and after values, which makes review easier

It uses the framework's own hook, so no extra audit library

Cons

Bulk updates and raw SQL can bypass the hook and skip the record

You pass the current actor in yourself from the request context

A large batch of changes slows the save and grows the audit table

Dedicated audit library (Audit.NET / django-simple-history)

Self-hosted open sourceOpen sourceOpsLow

Use a ready-made audit library directly; configure it its own way to record data changes automatically and avoid reinventing the wheel.

Best forTeams that want complete change history live quickly without maintaining audit code themselves.

Avoid whenAudit needs are very simple, or compliance requirements strictly constrain the record format.

Pros

Works out of the box, with no need to design the audit table and recording logic from scratch

django-simple-history can also view and restore historical versions

Audit.NET can output records to many destinations (follow the official docs for specifics)

Cons

You must learn the library's configuration style and watch compatibility on upgrades

django-simple-history creates a history table for every tracked model, so data grows quickly

The library's default record format may not meet your compliance requirements and needs further customization

Official docs ↗

Database triggers

Self-hosted open sourceOpen sourceOpsMedium

Set up "triggers" in the database: whenever anyone changes a table, the database itself automatically writes a record to the audit table.

Best forCases where multiple programs or people modify the same database directly and a fallback record is needed.

Avoid whenThe team is not familiar with database scripts, or business semantics must be recorded.

Pros

Changes from the app, a script, or a person editing the database are all recorded

It does not depend on the application language, and business code stays unchanged

Even if someone bypasses the app and edits the database, the record still holds

Cons

You usually know the database account, not the application user

The logic hides in the database, so both migration and debugging are harder

Every write takes one extra step, which costs performance when volume is high

Checklist

  • There is an explicit list of operations that must be audited
  • Each record has actor, time, target, result, and source IP
  • Audit records are append-only; ordinary business accounts cannot change or delete them
  • Passwords, tokens, and other sensitive plaintext are not recorded
  • Critical operations and audit rows commit in the same transaction
  • Retention period, access control, and anomaly alerts are in place