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.
2 Installation and Setup
First, we need to install the Laravel activity log package using the following command:
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:
By running the previous command, the activitilog.php file will be created where we can find different activity log 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.
- table_name – a parameter used to specify the activity 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.
3 Logging Model Events
Laravel activity log package can automatically log events such as a model that is created, updated or deleted. In our User.php model, we will use the 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:
Customizing the description:
By setting the all attributes our User model looks like:
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:
4 Recording login information of the users
As we have already mentioned before, there are pre-defined 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
4.1 Create an Event Listener
For creating an Event Listener, we need to run the following command:
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 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.
4.2 Map the Listener
Once we created the listener, we should attach it to the EventServiceProvider.php file.
Now we can record all login activity of the user in the activity log table. When the user will login into the application a new record will be created in our table.