Logging Activity Using Spatie/Laravel-Activitylog

20 Apr, 2020 | 4 minutes read


A good Senior PHP Programmer builds modern applications in a way to provide full transparency of the user interaction, especially in a case when there are many complex administration panels where one or more users can perform the same action on the same entity. In an environment like this, it is very important to track the user actions and provide an activity log where these actions can be stored, maintained and used for appropriate reporting.

In this blog post, we will describe how we can do this easily by using an external vendor for this purpose. When we want to log or record the activity of a user we can achieve that by using a package called laravel-activity-log created by the Spatie. The spatie/laravel-activity package provides easy to use functions to log the activities of our application users. It can also automatically log model events. The entire activity will be stored in the activity_log table. So, let’s start the Spatie activity log example and see everything else about the Laravel Spatie activity log tutorial.

Installation and Setup

First, we need to install the Laravel activity log package using the following command:

Installing Laravely activity log package

After the installation of the package we need to publish the migration, create table activity_log and publish the config file by running the following commands:

Publish the migration, create table activity_log and publish the config file
Publish the migration, create table activity_log and publish the config file
Publish the migration, create table activity_log and publish the config file

By running the previous command, the activitilog.php file will be created where we can find different activity log Laravel configuration parameters. There are already several predefined parameters that can be very useful such as:

  • enabled – If set to false, no activities will be saved to the database.
    – This can be very useful if we want to disable the logging mechanism, for example in our local environment.
  • delete_records_older_than_days – a parameter used to automatically clean up older activity logs Laravel.
  • table_name – a parameter used to specify the activity Laravel log table
    – By default, this is set to activity_log

We can modify these parameters as we need or add additional based on the functionality we are building.

Logging Model Events

Laravel activity log package can automatically log events such as a Laravel model log that is created, updated or deleted. In our User.php model, we will use the Spatie LogsActivity trait. This trait is extremely simple and has the static boot function for any model, as well as for registering an event that we described. For each event, the related query is performed and it is logged inside the database.

In our model we can set what attributes we will be logging by using:

If our model contains attributes that we want to ignore on a trigger change, we can use:

By default, this logger is configured to log every attribute of the model, but there is an easy way to change that. We can log only the changed attributes after the update by using the following configuration:

Customizing the events being logged

By default, the package will log the created, updated, deleted events, but we can customize with:

This image has an empty alt attribute; its file name is image-31.png

Customizing the description:

This image has an empty alt attribute; its file name is image-32.png

By setting the all attributes our User model looks like:

User model

Our Laravel activity log installation, setup, and logging model events are completed and we can test it by creating, updating or deleting a user in our application.

Create a user:

creating a user

activity_log table:

activity_log table

activity_log table:

activity_log table

Recording login information of the users

As we have already mentioned before, there are pre-defined Spatie logging events that are out of the box with this package. However, sometimes we want to log data on successful login or log out of the users in our application. This is also possible and easy to achieve and we need to follow the following steps:

  • Create an Event Listener
  • Map the Event Listener to the EventServiceProvider

1. Create an Event Listener

For creating an Event Listener, we need to run the following command:

Creating an Event Listener

After the new Event Listener is created, insert the following code into the LoginSuccessful listener:

As we can see from the code above, we are going to simply provide the subject and the description of the Laravel log activity, and after that, we will simply flush the success message to the end-user and store the data into the activity_log table.

2. Map the Listener

Once we created the listener, we should attach it to the EventServiceProvider.php file.

Attaching the Event Listener to the Event Service Provider

Now we can record all login activity of the user in the log activity Laravel table. When the user will login into the application a new record will be created in our table.

So, if you want to grow even more, don’t hesitate to watch a Spatie activity log tutorial again and again or any spatie/laravel-activitylog example to improve continuously.