Audit log (audit-log)
Records who created, updated or deleted what, and when, with a field-level diff. Hidden, write-only and password fields are never written to the log: only the fact that they changed.
npx calm add audit-log| Adds | |
|---|---|
| Files | app/blocks/audit-log/index.js |
| Environment | AUDIT_RETENTION_DAYS=365 |
| Collection | calm_audit_log (entries expire after the retention period) |
| Route | GET /api/audit-log (admin) |
Every resource write is recorded automatically, because the block listens to the <resource>.created, .updated and .deleted events that services emit. That includes writes from jobs and scripts that go through services.
Reading the log#
curl -g "http://localhost:5001/api/audit-log?resource=post&recordId=6acb46a57e4a738ebdd998b4" -H "Authorization: Bearer $ADMIN_TOKEN"{
"data": [
{
"id": "6acb5b2f7e4a738ebdd998e1",
"user": { "id": "6acb46a57e4a738ebdd998b1", "name": "Ada Lovelace" },
"action": "updated",
"resource": "post",
"recordId": "6acb46a57e4a738ebdd998b4",
"changes": {
"title": { "from": "Hello CalmAPI", "to": "Hello again" },
"internalNote": { "changed": true }
},
"requestId": "3e0c7a51-1d4b-4a9c-8f55-6f2d0b1c9e27",
"ip": "127.0.0.1",
"at": "2026-10-11T09:02:11.204Z"
}
]
}Filters: resource, recordId, action (created, updated, deleted), user (a user id) and limit (default 50, at most 200). Entries are newest first.
Options#
- Exclude a resource with
audit: falsein its options:resource('session-note', { audit: false, fields: { ... } }). - Change the retention with
AUDIT_RETENTION_DAYS. It is the TTL of an index, so once the index exists, changing the value also means updating that index in MongoDB (collMod). - The model is available as
app.ext.auditfor your own queries or an export job.
Official documentation#
Read these before changing the block; they are also in app/blocks/audit-log/AGENTS.md and in the devtools Blocks panel.
- OWASP Logging Cheat Sheet
- OWASP Logging Vocabulary Cheat Sheet
- MongoDB TTL indexes
- Mongoose: populate
For AI agents#
Coding agents read these notes in app/blocks/audit-log/AGENTS.md:
- Read first:
app/blocks/audit-log/index.js(under 80 lines). - What it does: a
calm.on('*')listener records every<resource>.created|updated|deletedevent intocalm_audit_logas{ action, resource, recordId, user, changes, requestId, ip, at }.app.ext.auditis the Mongoose model. - Reading:
GET /api/audit-log(policyadmin) withresource,recordId,action,userandlimit(max 200) query filters. - Configuration: env
AUDIT_RETENTION_DAYS(default 365) sets the TTL index onat. Nocalm.config.jsoptions. MongoDB keeps the oldexpireAfterSecondsof an existing index, so changing the value later needs acollMod(or dropping theat_1index). - Invariant:
diff()stores values only for fields withfield.isOutput; hidden, write-only and password fields are recorded as{ changed: true }. Never store raw values of those fields, and never logpayload.doc. - Invariant: the read route stays admin only: entries hold previous values of visible fields.
- Opt a resource out:
resource('x', { audit: false, ... }). - Only writes that go through resource services emit events.
model.updateOne()ormodel.collectioncalls are not audited: usectx.serviceorapp.servicesfor writes that must appear in the trail. - Custom entry (for example a login or an export):
await app.ext.audit.create({ action, resource, recordId, user, changes });actionmust becreated,updatedordeleted(schema enum). - Privacy: the privacy block erases accounts without per record events on purpose. To also remove a deleted user's audit entries, add
calm_audit_log: 'user'toblocks.privacy.alsoDeleteif your retention policy allows it. - Pitfall:
requestIdandipcome fromcurrentRequest(), so writes made by jobs or scripts have none. - Pitfall: the listener runs on every event; keep it cheap, and add an index before adding a new query filter (existing:
resource + recordId + at,user + at).