In the world of software development, keeping track of data changes is a common need for most big projects. Whether it’s keeping an eye on what users are doing or debugging issues, having a reliable activity log can make all the difference.
Depending on the amount of data the application is handling, activity logs can pile up pretty fast. That’s where optimization for both writing and reading these logs becomes crucial. Today, we’re excited to introduce Loggastic, an open-source library made by Locastic and designed to simplify the implementation of activity logs and store them in Elasticsearch.
What is Loggastic?
Loggastic is a library built on top of the Symfony framework. It allows you to easily track changes made on your objects and their relations, while letting you choose which fields you want to track.
Thanks to Elasticsearch’s speed in reading and searching data, you can easily filter and present your logs to end-users without the worry of a slow response, even as your activity logs continue to grow significantly.
How does the logging work?
The library comes with a built-in Doctrine event listener which tracks all Create, Edit and Delete actions made on loggable objects. The listener will detect when some action happens and will dispatch a message to the Symfony Messenger.
Message handler takes over and handles all required logic and saves logs to the ElasticSearch index. By default, messages are consumed synchronously and it’s up to you to configure a transport and make them asynchronous for better performance. All that’s needed is your input on the messenger configuration; the rest of the code is set to go.
Which data is stored in activity logs?
Activity log contains datetime, user who made the change, request url and before/after values. We store before and after values exclusively for the fields that have been altered. This approach not only conserves memory but also enhances log readability by avoiding the storage of redundant full object values.
For each loggable class, two indexes are created in the ElasticSearch: class_name_acivity_log & class_name_current_data_tracker.
The class_name_acivity_log one is obvious: we store all activity logs related to that class here.
The class_name_current_data_tracker is used for storing the latest values for all properties we track in this class, including tracked fields from relations. This is used to easily compare changed objects with previous values before storing activity_log before & after values. After activity_log is saved, current_data_tracker is updated with fresh data.
Reading activity logs
The bundle is ready to use ApiPlatform ElasticSearch integration in order to easily expose activity logs in API endpoints, taking advantage of built-in ApiPlatform filters, pagination and all other features.
It also comes with a service for pulling activity logs to the object which can be used to show them in a Twig view.
Example of activity logs view:
You can easily modify the default flow to align with project-specific requirements. This includes the ability to disable the standard listener, introduce your custom one, or initiate immediate logging upon changes. Additionally, logging extends beyond the conventional create, edit, or delete actions. So you are able to log actions not related to the database changes like user login, PDF download and similar. For detailed customization examples, refer to the bundle documentation.
Let us know what you think!
Head over to the Loggastic GitHub repository and try it out! If you have insights or ideas for improvement, we’re all ears! Open an issue, submit a PR, and become a part of this open source project. We value your participation and feedback!