togo install togo-framework/audit
audit is the togo answer to Spatie Activitylog / PaperTrail / django-reversion. It records an activity log of changes to your models — the action, the subject, the actor (causer), a field-level diff, and request IP/User-Agent — plus optional model versioning with restore.
- Activity log —
Log/LogChangewrite append-only entries toactivity_log, indexed by subject, causer and action. - Automatic diffs —
LogChange(old, new)stores only the fields that changed, as{old, new}. - Actor capture — the bundled
Middlewarepulls the causer (X-User-Id), client IP and User-Agent from the request into context, so logs are attributed automatically. - Model versioning —
Snapshotstores point-in-time JSON snapshots;Restorereturns prior state to re-apply. - Queryable — filter by subject / causer / action / time, with pagination, over a Go API and REST.
import "github.com/togo-framework/audit" a, _ := audit.FromKernel(k) // Log an action with an explicit changeset: a.Log(ctx, "created", "post", post.ID, userID, map[string]any{"title": post.Title}) // Or auto-diff old → new (stores only changed fields): a.LogChange(ctx, "updated", "post", post.ID, userID, oldMap, newMap) // Versioning + restore: a.Snapshot(ctx, "post", post.ID, oldMap, userID) // snapshot before a change data, ver, _ := a.Restore(ctx, versionID) // get prior state to re-apply // Query the trail: a.Activity(ctx, audit.Filter{SubjectType: "post", SubjectID: post.ID}) a.Activity(ctx, audit.Filter{CauserID: userID, Action: "deleted", Limit: 50})
Causer / IP / User-Agent are filled from the request context automatically (the plugin mounts audit.Middleware on the kernel router).
| Method | Path | Purpose |
|---|---|---|
GET |
/api/audit/activity |
filtered activity (subject_type, subject_id, causer_id, action, limit, offset) |
GET |
/api/audit/subjects/{type}/{id} |
a record's full history |
GET |
/api/audit/versions/{type}/{id} |
snapshots for a subject |
POST |
/api/audit/versions/{id}/restore |
snapshot data to re-apply |
GET |
/api/audit/dashboard |
recent activity feed |
No required env. Audit creates its tables (activity_log, versions) on boot via the kernel database (works on SQLite/Postgres/MySQL through togo's ORM). High-volume deployments can partition activity_log by created_at.