mradder/filament-logger
Community-maintained Filament audit/activity logging built on spatie/laravel-activitylog. Includes a ready-made Activity resource with filters and diffs, CSV/JSON exports, dashboard widgets, and automatic logging for resources, models, auth, notifications, and custom events.
Installation:
composer require mradder/filament-logger
php artisan filament-logger:install
php artisan migrate
config/filament-logger.php) and Spatie ActivityLog migrations.Register the Activity Resource:
Add to your PanelProvider:
public function panel(Panel $panel): Panel {
return $panel->resources([
config('filament-logger.activity_resource'),
]);
}
First Use Case:
Automatic Resource Logging:
HasActivityLog trait:
use MrAdder\FilamentLogger\Concerns\HasActivityLog;
class PostResource extends Resource {
use HasActivityLog;
}
Custom Model Logging:
getActivitylogOptions() in your model:
public function getActivitylogOptions(): array {
return [
'only' => ['title', 'content'], // Log only these fields
'ignore' => ['updated_at'], // Exclude sensitive fields
];
}
Auth Event Tracking:
'access' => [
'guards' => ['web'], // Only log auth events for the 'web' guard
]
Custom Domain Events:
OrderShipped):
use MrAdder\FilamentLogger\Facades\FilamentLogger;
event(new OrderShipped($order));
FilamentLogger::logEvent('order.shipped', $order);
Alerting:
FilamentLogger::alert('user.deleted', [
'user_id' => $user->id,
'count' => $deletedCount,
]);
config/filament-logger.php:
'alerts' => [
'channels' => ['mail', 'slack'],
'rules' => [
'user.deleted' => [
'cooldown' => 3600, // 1-hour cooldown
],
],
]
PanelProvider:
->widgets([
\MrAdder\FilamentLogger\Widgets\ActivityOverview::class,
\MrAdder\FilamentLogger\Widgets\TopUsers::class,
])
php artisan filament-logger:prune --days=30 --log-names=activity
Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule) {
$schedule->command('filament-logger:prune')->daily();
}
Authorization Strictness:
use MrAdder\FilamentLogger\Models\Activity;
Gate::define('viewActivity', fn ($user) => $user->can('view-activity'));
'strict_authorization' => false,
Sensitive Data Exposure:
ignore in getActivitylogOptions() to exclude fields like api_token:
'ignore' => ['api_token', 'password'],
Performance:
deleteMany) may generate many logs. Use ignore for non-critical fields:
'ignore' => ['updated_at', 'deleted_at'],
Custom Events:
User with orders relationship).->toArray() or json_encode() for complex data:
FilamentLogger::logEvent('custom.event', $data->toArray());
Missing Logs:
HasActivityLog or observer is registered.config/filament-logger.php for log_models/log_resources settings.'debug' => true,
Alerts Not Triggering:
storage/logs/laravel.log for alert dispatch errors.FilamentLogger::alert('test.alert', ['message' => 'Hello']);
Diff View Issues:
ignore for verbose fields.json_encode($data, JSON_THROW_ON_ERROR).Custom Log Labels: Override labels in config:
'labels' => [
'create' => 'Record Created',
'update' => 'Record Updated',
],
Additional Widgets:
Extend the Widget class to add custom dashboards:
class CustomActivityWidget extends Widget {
protected static string $view = 'filament-logger::widgets.custom';
// ...
}
Pre-Log Hooks:
Intercept logs via the activity.logging event:
Event::listen(\Spatie\Activitylog\Events\Logging::class, function ($event) {
if ($event->logName === 'activity') {
$event->cancel(); // Skip logging
}
});
Retention Policies: Customize pruning in config:
'pruning' => [
'activity' => [
'days' => 90,
'dry_run' => false,
],
],
'anonymize_ips' => true,
filament-logger::labels).$this->partialMock(Spatie\Activitylog\Activity::class, function ($mock) {
$mock->shouldReceive('log')->once();
});
```markdown
### Configuration Quirks
1. **Multi-Guard Apps**:
- Auth events **default to all guards**. Explicitly whitelist guards in config:
```php
'access' => [
'guards' => ['web', 'api'], // Only log these guards
]
```
- Events like `PasswordReset` **ignore guards** and always log.
2. **Notification Logging**:
- Disabled by default for security. Enable with:
```php
'notifications' => [
'log_recipients' => true,
'mask_recipients' => true, // Redact email addresses
]
```
3. **Legacy Filament 3**:
- Uses **shims** for compatibility but may miss newer Filament 4/5 features.
- Prefer Filament 4.3.1+ for full support.
4. **Database Indexes**:
- The `activity_logs` table **auto-creates indexes** for `log_name`, `properties`, and `created_at`.
- For large datasets, add custom indexes (e.g., `user_id`):
```php
Schema::table('activity_logs', function (Blueprint $table) {
$table->index('properties->user_id');
});
```
How can I help you explore Laravel packages today?