Skip to main content
The AuditRails PHP SDK is a PSR-18 compatible client library for PHP 8.1 and later. Rather than bundling an HTTP client, it accepts any PSR-18 compatible implementation — Guzzle, Symfony HttpClient, or any other library you already have in your project. Events are buffered in memory and flushed in the background via register_shutdown_function, so your application code is never blocked by network I/O and no events are lost when the request ends.

Installation

You also need a PSR-18 HTTP client and PSR-7 HTTP factories. If you are not already using one, Guzzle is a common choice:
Requires PHP 8.1+.

Initialization

Construct an AuditRails instance by passing a Config object and your PSR-18 HTTP client along with its request and stream factories.
Create one instance per application lifecycle (e.g. in a service container) and reuse it throughout. The autoShutdown option (enabled by default) registers a register_shutdown_function that automatically flushes buffered events at the end of every PHP request or script.

Configuration options

Logging events

$audit->log() adds an AuditEvent to the in-memory buffer and returns immediately. The buffer is automatically flushed at the end of the request. This method never throws.

Direct (immediate) logging

$audit->logDirect() sends the event immediately and returns the API response. It throws AuditRailsError on failure.

Direct batch logging

Send multiple events in a single HTTP request, bypassing the buffer.

Manual flush

Framework integration

Laravel

Service provider registration Bind AuditRails as a singleton in your AppServiceProvider (or a dedicated AuditServiceProvider). Laravel resolves Guzzle via its built-in HTTP client, so you can pull the factory from the container.
Middleware Create a reusable middleware that accepts the audit action as a route-level parameter:
Register the middleware alias in bootstrap/app.php (Laravel 11+) or Kernel.php (Laravel 10), then apply it to routes:

Error handling

log() never throws. For logDirect() and logBatchDirect(), catch AuditRailsError to inspect the failure details.

PSR-3 logging

Pass any PSR-3 compatible logger as the logger argument to receive internal SDK diagnostics (retry attempts, flush failures, etc.):

Graceful shutdown

When autoShutdown is true (the default), the SDK calls register_shutdown_function and flushes all buffered events automatically at the end of every PHP request or CLI script. No additional configuration is required in most applications. To flush manually — for example after a large import job or before a planned maintenance window — call:
To flush and explicitly tear down the client (useful in long-running CLI commands or queue workers):
In long-running PHP processes such as queue workers (Laravel Horizon, ReactPHP, Swoole), each job runs in the same PHP process. register_shutdown_function fires only when the process itself exits, not between jobs. Call $audit->flush() at the end of each job to ensure events from that job are delivered promptly.