Building an admin panel usually requires some form of logging. This means you must log every change or create actions on your application. The question is - how do you do this with Filament? You have a few package options, so let's look at them.
In this tutorial, we will look at 3 different Filament logging packages and compare them. We will look at:
- https://filamentphp.com/plugins/z3d0x-logger
- https://filamentphp.com/plugins/pxlrbt-activity-log
- https://filamentphp.com/plugins/noxoua-activity-log
Package 1: z3d0x/filament-logger
Installing this package is pretty easy:
composer require z3d0x/filament-logger
Then we run the installation command:
php artisan filament-logger:install
Before going deeper - we have to prepare our Models using Spatie documentation:
Model
use Spatie\Activitylog\Traits\LogsActivity; // ... use LogsActivity; // ... public function getActivitylogOptions(): LogOptions{ return LogOptions::defaults() ->logFillable();}
Last, we have to add a Resource to our PanelProvider:
app/Providers/Filament/AdminPanelProvider.php
public function panel(Panel $panel): Panel{ return $panel // ... ->resources([ config('filament-logger.activity_resource') ]) // ...}
That's it. If we open our Panel, we will see a new Activity Log resource in the Sidebar. Opening it after we have made some changes to our Model, we will see the following:
This table has all the filters to quickly find a resource we need. And if we click on the row - we can see all the details:
That's it! We have working logs now. And here's what we think about this package:
The Good
- Easy to install
- Gives all the information that you want
- Good table filters
- Nice UI
- Works out of the box with all models
- Easy to customize, as you can override the Resource for logs and implement anything you need
The Bad
- Requires database columns to match the labels. If there's a mismatch - it might get confusing
- Does not have many out-of-the-box configuration options
Overall
If we look at this from the perspective of having something in our application - we have a strong candidate here. While it might not be the best on our list, it works well.
Package 2: pxlrbt/filament-activity-log
This package is a little harder to install as we must create a new page. Still, it starts with a simple installation of the Spatie Activity Log and Activity Log package:
composer require spatie/laravel-activitylogcomposer require pxlrbt/filament-activity-log
Then, we have to first prepare our Models using Spatie documentation:
Model
use Spatie\Activitylog\Traits\LogsActivity; // ... use LogsActivity; // ... public function getActivitylogOptions(): LogOptions{ return LogOptions::defaults() ->logFillable();}
Next, we need to create a new page for our Activity Log:
php artisan make:filament-page ActivityLogPage --resource=CustomerResource --type=custom
Then we have to modify that page:
app/Filament/Resources/CustomerResource/Pages/ActivityLogPage.php
use pxlrbt\FilamentActivityLog\Pages\ListActivities; // ... class ActivityLogPage extends Pageclass ActivityLogPage extends ListActivities{ protected static string $resource = CustomerResource::class; protected static string $view = 'filament.resources.customer-resource.pages.activity-log-page';}
And last, we have to add a button to our Resource to link to this page, along with URL registration:
app/Filament/Resources/CustomerResource/CustomerResource.php
// ... ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\Action::make('activities')->url(fn($record) => CustomerResource::getUrl('activities', ['record' => $record])),// ... public static function getPages(): array{ return [ 'index' => Pages\ListCustomers::route('/'), 'create' => Pages\CreateCustomer::route('/create'), 'edit' => Pages\EditCustomer::route('/{record}/edit'), 'activities' => Pages\ActivityLogPage::route('/{record}/activities'), ];} // ...
Now, visiting our table, we should see a new button:
This will open our Resource activity log page:
That's it. It does work fine, and here are our thoughts:
The Good
- Average installation complexity
- Gives you a nice page for specific Model logs
- Gives us a
Restore
button that will restore the Model to the state of the log - the only one that did this! - Works quite well overall and displays changes in a readable format
The Bad
- Has some UI issues (as seen in the screenshot)
- Does not have many out-of-the-box configuration options
- Requires your users to have an Avatar (we did not have one, so it caused an error)
Overall
This package is a good candidate for your project if you want per-resource-based logs. This means that you are okay with creating each resource log individually. It also has a great feature of restoring the Model to the state of the log, which is a great feature to have.
Package 3: noxoua/filament-activity-log
To install this package, we have to run the following command:
composer require noxoua/filament-activity-log
Then we have optional commands to publish the config and views:
php artisan vendor:publish --tag="filament-activity-log-config"php artisan vendor:publish --tag="filament-activity-log-views"php artisan vendor:publish --tag="filament-activity-log-translations"
While not required, they will allow you to modify the package to your needs. Next, we have to add a new page:
php artisan make:filament-page ViewActivityLog
This page should not belong to any resources, as it's a custom global page. So we have to modify it:
app/Filament/Pages/ViewActivityLog.php
use Noxo\FilamentActivityLog\Pages\ListActivities; // ... class ViewActivityLog extends Pageclass ViewActivityLog extends ListActivities{ protected static ?string $navigationIcon = 'heroicon-o-document-text'; protected static string $view = 'filament.pages.view-activity-lo';}
Next, we have to create a logger:
php artisan make:filament-logger CustomerLogger
And modify it:
app/Filament/Loggers/CustomerLogger.php
class CustomerLogger extends Logger{ public static ?string $model = Customer::class; public static function getLabel(): string|Htmlable|null { return CustomerResource::getModelLabel(); } public static function resource(ResourceLogger $logger): ResourceLogger { return $logger ->fields([ // Here you have to define all of your fields to be logged Field::make('employee.name'), Field::make('first_name'), Field::make('last_name'), Field::make('email'), Field::make('phone_number'), Field::make('description'), Field::make('lead_source_id'), Field::make('tags'), Field::make('pipeline_stage_id'), ]) ->relationManagers([ // ]); }}
Now, once this is done, we can create and update the Model to see the logs:
Next, if we want to filter to a specific record, we can use the filter at the top:
That's it. We have logs now. Here are our thoughts:
The Good
- It's highly customizable
- Has great search capabilities
- Can be user-friendly if you tinker with it
- Has a nice UI
The Bad
- Requires a lot of manual work to set up - you have to create loggers for each of the resources and define all the fields to be logged
- Displays all entries in a single table, which can be confusing at first
Overall
Looking at this package, it's a great package. It's highly customizable and has a lot of potential. But it requires a lot of manual work to set up and make it work exactly as you might need.
What to Pick?
Since activity logs are highly customizable, there's no one-size-fits-all solution. But we can give you some pointers on what to pick:
Do you need something fast and easy? Pick z3d0x/filament-logger
. It's easy to install and works out of the box.
Do you need something that will give you per-resource logs? Pick pxlrbt/filament-activity-log
. It's easy to install and gives you an excellent page for each resource.
Do you need something that will give you a lot of customization? Pick noxoua/filament-activity-log
. It's highly customizable and has a lot of potential.
No comments or questions yet...