register_shutdown_function, so your application code is never blocked by network I/O and no events are lost when the request ends.
Installation
Initialization
Construct anAuditRails instance by passing a Config object and your PSR-18 HTTP client along with its request and stream factories.
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
Buffered logging (recommended)
$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 BindAuditRails 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.
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 thelogger argument to receive internal SDK diagnostics (retry attempts, flush failures, etc.):
Graceful shutdown
WhenautoShutdown 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:
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.