@setu-ts/audit-plugin — record immutable audit events
Add it: setu add audit adds and registers AuditPlugin() in setu.config.ts with in-memory
storage. It provides CAPABILITIES.AUDIT (IAuditLogger).
Record a completed operation
import { CAPABILITIES } from '@setu-ts/common';
import type { IAuditLogger } from '@setu-ts/common';
const audit = ctx.services.get<IAuditLogger>(CAPABILITIES.AUDIT);
await audit.log({
action: 'account.disable',
resource: 'account',
resourceId: ctx.params.id,
result: 'success',
before: { active: true },
after: { active: false },
});
Keep events in a file
Replace the registration in setu.config.ts:
import { AuditPlugin } from '@setu-ts/audit-plugin';
export const audit = AuditPlugin({
storage: 'file',
options: { path: './var/log/audit.log' },
});
Behaviour the types do not show
- Memory storage is the default and is lost on restart.
- Each record gets an id and timestamp from the runtime and is deep-frozen before storage.
logpropagates storage failures; it does not silently discard failed writes.- Configured
redactionfiltersbefore,afterandmetadatabefore freezing the record. - File storage requires writable
runtime.fs; registration fails when it is absent. File writes are serialized read-modify-write operations. - Database storage requires an injected
options.client; no driver is loaded automatically.
Other options
storage: 'log'usesoptions.loggeror the logger capability;options.leveldefaults to info.storage: 'database'takes anIAuditDbClient;options.tabledefaults toaudit_logs.AuditQueryandStoredAuditEntrydescribe queries on the exported storage classes.