Getting Started
Quickstart
Get go-audit running in under 5 minutes. This guide walks through installation, configuration and recording your first audit log entry.
Install the package
Pull the module into your Go project. The library has zero non-stdlib dependencies for the core, ORM adapters are opt-in.
$ go get github.com/gopackx/go-auditRequires Go 1.21+
Initialize the auditor
Wire up an Auditor with your *sql.DB and a UserFunc that resolves the actor for each transaction.
main.go
import "github.com/gopackx/go-audit"
auditor, err := audit.New(sqlDB, audit.Config{
Dialect: audit.SQLite,
UserFunc: func(ctx context.Context) (string, string) {
return ctx.Value("user_id").(string), "user"
},
})Run migrations
Create the audit_log and audit_snapshots tables. Safe to call on every boot — it is idempotent.
_ = auditor.AutoMigrate(ctx)Plug into GORM
Register the GORM plugin once on your *gorm.DB. From there, every Create/Update/Delete is recorded automatically.
_ = gormDB.Use(auditgorm.Plugin(auditor))Bun and Ent adapters are also available — see ORM Adapters.
Verify it works
Quick sanity check that everything is wired up end-to-end.
- 1Create or update a record through GORM inside a request handler.
- 2Query
auditor.History(ctx, "users", id)and confirm a row is returned. - 3Inspect the diff JSON column — you should see before/after values for each changed field.