cetiia/laravel-activity-log
Laravel package that automatically records application activity to a logs table after migration and provides an activity-log route to view entries. Includes publishable migrations and optional views; access to the route can be protected via a Gate in AuthServiceProvider.
Installation
composer require cetiia/laravel-activity-log
Publish the migration and config:
php artisan vendor:publish --provider="Cetiia\ActivityLog\ActivityLogServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Cetiia\ActivityLog\ActivityLogServiceProvider" --tag="config"
Run the migration:
php artisan migrate
Basic Usage
Log an activity for a model (e.g., User):
use Cetiia\ActivityLog\ActivityLog;
// Log a custom event
ActivityLog::log('user.created', 'User created', ['user_id' => 1]);
// Log model events (e.g., created, updated, deleted)
ActivityLog::logModelEvent('user', 'created', ['user_id' => 1]);
First Use Case Track user sign-ups and admin actions:
// In UserObserver or controller
ActivityLog::log('auth.login', 'User logged in', ['user_id' => auth()->id()]);
Leverage Laravel’s observers to auto-log model events:
// app/Observers/UserObserver.php
public function created(User $user)
{
ActivityLog::logModelEvent('user', 'created', ['user_id' => $user->id]);
}
Log API requests or controller actions:
// app/Http/Middleware/LogActivity.php
public function handle($request, Closure $next)
{
ActivityLog::log('api.request', $request->method() . ' ' . $request->path(), [
'ip' => $request->ip(),
'user_id' => auth()->id(),
]);
return $next($request);
}
Log database queries (e.g., for auditing):
// Before a critical query
ActivityLog::log('query.executed', 'Fetching users', ['query' => $query->toSql()]);
$users = User::where('active', true)->get();
Attach to Laravel events (e.g., illuminate\auth\events\Registered):
// app/Listeners/LogUserRegistration.php
public function handle($event)
{
ActivityLog::log('user.registered', 'New user registered', ['email' => $event->user->email]);
}
Extend the package for domain-specific logs:
// app/Services/CustomActivityLogger.php
class CustomActivityLogger
{
public static function logPayment($paymentId, $amount, $status)
{
ActivityLog::log('payment.processed', "Payment {$status}", [
'payment_id' => $paymentId,
'amount' => $amount,
]);
}
}
Performance Overhead
$users = User::all();
ActivityLog::log('batch.processed', 'Processed ' . count($users) . ' users', ['batch_id' => uniqid()]);
Missing Model Events
AppServiceProvider:
User::observe(UserObserver::class);
Config Quirks
activity_log table name is configurable in config/activity-log.php. Update if using a custom schema.Debugging Logs
activity_logs table directly or use:
$logs = \Cetiia\ActivityLog\Models\ActivityLog::latest()->take(10)->get();
Custom Fields
Add columns to the activity_logs table via migration:
Schema::table('activity_logs', function (Blueprint $table) {
$table->string('metadata')->nullable();
});
Log Filtering Filter logs by type or properties in queries:
$logs = ActivityLog::where('type', 'user.created')->where('properties->user_id', 1)->get();
Soft Deletes
Enable soft deletes in the ActivityLog model if needed:
use Illuminate\Database\Eloquent\SoftDeletes;
class ActivityLog extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Queue Integration Offload logging to a queue for async processing:
ActivityLog::dispatch('user.created')->toQueue('activity-logs');
Requires extending the ActivityLog facade or service provider.
auth.login, payment.refunded) for clarity.deleted_at column and a scheduled job to purge old logs:
// app/Console/Commands/CleanupActivityLogs.php
public function handle()
{
\Cetiia\ActivityLog\Models\ActivityLog::where('created_at', '<', now()->subDays(30))->delete();
}
How can I help you explore Laravel packages today?