spatie/laravel-activitylog
Log user and model activity in Laravel with a simple API. Automatically record Eloquent events, track subjects and causers, attach custom properties, and query everything via the Activity model. Stores logs in the activity_log table.
By default, each activity is saved to the database immediately with its own INSERT query. If your application logs many activities during a single request (for example, when updating multiple models in a loop), this can result in a significant number of queries.
When buffering is enabled, activities are collected in memory during the request and inserted in a single bulk query after the response has been sent to the client.
You should only enable buffering if your application logs a high volume of activities per request. For most applications that log just a handful of activities per request, the default behavior is perfectly fine.
Buffering is most useful when:
Add this to your .env file:
ACTIVITYLOG_BUFFER_ENABLED=true
Or set it directly in config/activitylog.php:
'buffer' => [
'enabled' => true,
],
That's it. No other code changes are required. All existing logging code (both automatic model event logging and manual activity()->log() calls) will be buffered automatically.
When buffering is enabled:
terminating phase), all buffered activities are inserted in a single queryThe buffer also registers a shutdown function as a safety net, so activities are flushed even if the application terminates unexpectedly.
Buffered activities will not have a database ID until the buffer is flushed. If you need the activity ID immediately after logging, do not enable buffering.
$activity = activity()->log('some activity');
// With buffering enabled, $activity->id will be null here.
// The activity will be saved after the response is sent.
The buffer is registered as a scoped binding, which means it is automatically reset between requests in Laravel Octane. The terminating callback fires per request in Octane, so activities are flushed correctly.
The buffer is automatically flushed after each queued job completes (or fails). Activities logged within a job will be bulk inserted when that job finishes.
How can I help you explore Laravel packages today?