spatie/laravel-activitylog
Log user and model activity in Laravel with a simple API. Automatically record Eloquent model events, link actions to subjects and causers, store custom properties, and query a dedicated activity_log table for auditing and history.
beforeLogging hook and custom LogOptions allow for deep customization, such as enriching logs with metadata (e.g., IP addresses, request IDs) or filtering sensitive attributes. This makes it adaptable to niche use cases like batch processing or multi-tenant systems.activity_log table schema is simple and can be extended without breaking changes.LogsActivity trait enables automatic logging for Eloquent models with minimal code changes (e.g., use LogsActivity). This is a significant advantage for applications with hundreds of models, as it eliminates manual event listeners.Activity facade provides a fluent interface for manual logging, which is intuitive and reduces cognitive load for developers. Example:
activity()->performedOn($order)->causedBy($user)->log('Order updated');
logOnlyDirty() or dontLogIfAttributesChangedOnly() to minimize writes.beforeLogging hook to batch related activities (e.g., using batch_uuid).activity_log table can grow rapidly in high-activity systems. Solutions include:
Activity::all()) to replicas.activity_log table growth and performance?default, admin_actions, api_events) be needed, or is a single table sufficient?activity_log table grows?LogsActivity).CauserResolver and hooks.beforeLogging to batch activities).User, Order, Payment).User) to test the LogsActivity trait and manual logging.activity_log schema and adjust for custom ID types (e.g., UUIDs).beforeLogging hook to add metadata (e.g., request_id, user_agent).LogsActivity to additional models, starting with those with high change frequency.getActivitylogOptions() to optimize logging (e.g., logOnlyDirty).admin_log, api_log) if different audit requirements exist.Activity::latest()).composer test to catch regressions.activity_log table.composer require spatie/laravel-activitylog
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"
php artisan migrate
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-config"
config/activitylog.php if needed (e.g., default_log_name).use LogsActivity; to target models.getActivitylogOptions() for custom behavior.Log::info()) with activity()->log() where appropriate.beforeLogging callbacks in a service provider (e.g., AppServiceProvider).CauserResolver if default user resolution isn’t sufficient.activity_log table growth or slow queries.How can I help you explore Laravel packages today?