Hosmel Quintana
Laravel Audit Logs
Introduction
Installation
Configuration
Basic usage
Recording Audit Logs
Actors and Targets
Querying Audit Logs
Testing Audit Logs
Advanced usage
Batching and Correlating Logs
Customizing the Audit Log Model
View on GitHub

Configuration

Configure defaults, request metadata, retention, and storage.

The published config/audit-log.php file controls default attributes, request metadata, retention, and database storage.

Default attributes

audit-log.defaults.bucket and audit-log.defaults.source fill missing bucket and source values when a log is prepared. Explicit values always take precedence.

Request metadata

The package can fill missing remoteIp and userAgent values from the current request. Remote IP and user agent capture are enabled by default.

Console capture is disabled because Laravel binds a synthetic request while running Artisan. Enable audit-log.request.capture_in_console only when that request should be used.

Values set explicitly through remoteIp() or userAgent() are preserved.

Storage

audit-log.storage.connection and audit-log.storage.table select where logs are stored. A null connection uses Laravel's default database connection.

Batch inserts are split by audit-log.storage.insert_chunk_size. All chunks from one record() call are written inside the same database transaction. The chunk size must be greater than zero.

Retention and pruning

Set audit-log.retention.days to a non-negative number of days or null. A null value keeps new audit logs indefinitely.

When a log is written, its expires_at value is calculated from occurred_at using the current retention setting. Changing the setting does not update rows that already exist.

The package model uses Laravel's MassPrunable trait. Schedule model:prune to delete rows whose expires_at value has passed:

php
use HosmelQ\AuditLog\Models\AuditLog;
use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune', [
    '--model' => [AuditLog::class],
])->daily();

Only rows with a non-null, expired expires_at value are pruned.

Environment variables

Configuration keyEnvironment variableDefault
defaults.bucketAUDIT_LOG_DEFAULTS_BUCKETapplication
defaults.sourceAUDIT_LOG_DEFAULTS_SOURCEplatform
request.capture_in_consoleAUDIT_LOG_REQUEST_CAPTURE_IN_CONSOLEfalse
request.capture_remote_ipAUDIT_LOG_REQUEST_CAPTURE_REMOTE_IPtrue
request.capture_user_agentAUDIT_LOG_REQUEST_CAPTURE_USER_AGENTtrue
retention.daysAUDIT_LOG_RETENTION_DAYSnull
storage.connectionAUDIT_LOG_STORAGE_CONNECTIONnull
storage.insert_chunk_sizeAUDIT_LOG_STORAGE_INSERT_CHUNK_SIZE500
storage.tableAUDIT_LOG_STORAGE_TABLEaudit_logs
InstallationRecording Audit Logs