Skip to main content
The AuditRails Go SDK is a zero-dependency client built entirely on the standard library (net/http). It is designed to be idiomatic Go: context-aware, safe for concurrent use from multiple goroutines, and properly closeable via defer client.Close(). Events are buffered and flushed in a background goroutine so your hot paths are never blocked by network I/O.

Installation

Requires Go 1.21 or later.

Initialization

Create a client with auditrails.New(). The first argument is your API key; the second is an optional *Options struct. Always pair client creation with defer client.Close() to ensure buffered events are flushed when your application exits.
Pass nil as the second argument to use all defaults, or supply an *Options struct to customise behaviour:

Configuration options

Logging events

client.Log() appends an Event to the in-memory buffer and returns immediately. The buffer is flushed in the background on FlushInterval. This method never returns an error — failures are logged internally if a Logger is configured.

Context-aware buffered logging

client.LogWithContext() behaves identically to Log() but respects context cancellation. If the context is already done when the call is made, the event is silently dropped. Use this in request handlers where you want cancellation to propagate.

Direct (immediate) logging

client.LogDirect() sends the event immediately and returns the API response. It returns an error on failure.

Direct batch logging

Send multiple events in one HTTP request, bypassing the buffer.

Manual flush

Framework integration

Standard net/http middleware

Wrap any http.Handler with a middleware function to automatically audit every request that passes through it.
This pattern composes cleanly with any router that follows the standard http.Handler interface (Chi, Gorilla Mux, etc.).

Error handling

Log() and LogWithContext() never return errors. For LogDirect() and LogBatchDirect(), use errors.As to unwrap an *auditrails.APIError and inspect the structured fields.

Graceful shutdown

Calling client.Close() flushes all buffered events and stops the background goroutine. Always use defer client.Close() immediately after creating a client so shutdown is guaranteed even if your main function returns early.
For long-running services that handle OS signals, you can wire client.Close() into your signal handler as well:
client.Close() is safe to call multiple times. Subsequent calls after the first are no-ops.