auditlog

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

auditlog

Go Reference CI Latest Release

A PocketBase plugin that records an audit trail of the changes made to your collections.

[!WARNING] This plugin's admin UI is built on PocketBase's UI extensions mechanism, which is still under active development. Expect this plugin's UI to require breaking changes to keep up as PocketBase's own extension APIs evolve.

Installation

With xpb

Include the adapters/xpb submodule when building your PocketBase binary. It registers itself automatically, so no extra code is required:

xpb build --with github.com/pocketbasestore/auditlog/adapters/xpb

The adapter lives in its own Go module so that plain library consumers (below) don't pull in xpb's dependencies. It is configured through pocketbuilds.toml or environment variables — see Configuration.

As a library

Import the package and register it against your app instance. Use MustRegister to panic on failure, or Register to handle the error yourself:

package main

import (
	"log"

	"github.com/pocketbase/pocketbase"

	"github.com/pocketbasestore/auditlog"
)

func main() {
	app := pocketbase.New()

	auditlog.MustRegister(app)

	// or, handling the error explicitly:
	// if err := auditlog.Register(app); err != nil {
	// 	log.Fatal(err)
	// }

	if err := app.Start(); err != nil {
		log.Fatal(err)
	}
}

Settings are passed as registration options — see Configuration.

Configuration

Setting pocketbuilds.toml key (xpb only) Environment variable (xpb only) Registration option (library only) Default
Log retention, in days (0 = keep forever) initial_retention_days XPB__AUDITLOG__INITIAL_RETENTION_DAYS WithInitialRetentionDays(n) 30
Collections excluded from auditing (by name) initial_excluded_collections XPB__AUDITLOG__INITIAL_EXCLUDED_COLLECTIONS WithInitialExcludedCollections(names...) none
  • Log retention — how many days audit log entries are kept before the daily cleanup cron deletes them. 0 keeps them forever.
  • Excluded collections — every collection is audited by default; this is a blocklist of the ones to leave out. Collections are referenced by name and resolved to ids at seed time, so a name that doesn't match an existing collection at first boot is skipped with a warning.

Which mechanisms are available depends on how the plugin is installed, and the two sets don't overlap: a binary built with xpb reads pocketbuilds.toml and environment variables, while a library consumer passes registration options in code.

Whatever the source, these only seed a setting's initial value on first boot. Afterwards the values stored in the _auditLogSettings collection — editable from the plugin's settings modal in the admin UI — are the source of truth, and these sources are ignored.

With xpb, a setting can come from either source, and the precedence is env > pocketbuilds.toml > default:

# pocketbuilds.toml
[auditlog]
initial_retention_days = 60
initial_excluded_collections = ["posts", "comments"]
# environment variables
XPB__AUDITLOG__INITIAL_RETENTION_DAYS=60
XPB__AUDITLOG__INITIAL_EXCLUDED_COLLECTIONS=posts,comments

As a library, the equivalent is:

auditlog.MustRegister(app,
	auditlog.WithInitialRetentionDays(60),
	auditlog.WithInitialExcludedCollections("posts", "comments"),
)

Caveats

  • Best effort, not atomic. Audit log writes are best effort and are not transactionally tied to the change they describe. If writing the audit entry fails, the error is logged and the original operation still succeeds — so a change may occur without a corresponding audit log entry. Do not rely on this plugin as a guaranteed, tamper-proof record.
  • API changes only. Only record mutations that go through the REST API (create / update / delete request hooks) are logged. Changes made programmatically from Go (e.g. app.Save(...)), from cron jobs, or from migrations bypass these hooks and are not recorded.

Documentation

Overview

Package auditlog is a PocketBase plugin that records an audit trail of the changes made to your collections.

Register it against an app instance before calling app.Start:

app := pocketbase.New()
if err := auditlog.Register(app); err != nil {
	log.Fatal(err)
}

Or use MustRegister to panic on failure instead of handling the error.

Behaviour

Every collection is audited by default. On every boot the plugin applies its pending schema migrations, which on first boot create the "_auditLogs" and "_auditLogSettings" collections. It then seeds the settings row from the Option values passed to Register (or their defaults) and installs the hooks, crons, and routes that keep the audit trail running.

Migrations are tracked in the plugin's own "_auditLogMigrations" table, separate from PocketBase's "_migrations", so the host application's "migrate" commands never interfere with the plugin's schema history. After first boot, the values stored in "_auditLogSettings" — editable from the plugin's settings modal in the admin UI — are authoritative, and the registration options only seeded that initial row.

Only mutations that go through the REST API (create/update/delete request hooks) are recorded; changes made programmatically from Go (app.Save, cron jobs, migrations, ...) bypass those hooks and are not logged. Audit writes are also best effort and not transactionally tied to the change they describe: if writing the entry fails, the error is logged and the original operation still succeeds, so a change can occur without a corresponding audit log entry. Do not rely on this plugin as a guaranteed, tamper-proof record.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustRegister

func MustRegister(app core.App, opts ...Option)

MustRegister registers the audit plugin to the provided app instance and panics on error.

func Register

func Register(app core.App, opts ...Option) error

Register registers the audit plugin to the provided app instance.

Types

type Option

type Option func(*config)

Option configures the audit plugin.

func WithInitialExcludedCollections

func WithInitialExcludedCollections(collections ...string) Option

WithInitialExcludedCollections sets the collections (by name or id) excluded from auditing when the "_auditLogSettings" row is seeded on first boot only. Names that don't resolve to an existing collection at that point are skipped. After the first boot the value stored in the DB is authoritative and this is ignored.

func WithInitialRetentionDays

func WithInitialRetentionDays(days int) Option

WithInitialRetentionDays sets the retention window (in days) used to seed the "_auditLogSettings" row on first boot only. 0 means "keep forever". After the first boot the value stored in the DB is authoritative and this is ignored.

Directories

Path Synopsis
adapters
xpb module
Package main is a minimal PocketBase app with the auditlog plugin registered, used to try the plugin out locally.
Package main is a minimal PocketBase app with the auditlog plugin registered, used to try the plugin out locally.
internal
log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL