The @sourceloop/audit-log package is a powerful LoopBack 4 extension designed to seamlessly implement audit logs in your LoopBack applications. With this extension, you can effortlessly track and record audit data for all database transactions within your application.
This package provides a generic model that enables the storage of audit logs, which can be backed by any datasource of your choice. Whether you're using MySQL, PostgreSQL, MongoDB, or any other database, the Audit Logs package ensures compatibility and flexibility.
By incorporating the Audit Logs package into your application, you gain valuable insights into the history of data changes, user actions, and system events. Maintain a comprehensive audit trail for compliance, troubleshooting, and analysis purposes.
Using lb4 datasource command create your own datasource using your preferred connector. Here is an example of datasource using postgres connector. Notice the statement static dataSourceName = AuditDbSourceName;. Make sure you change the data source name as per this in order to ensure connection work from extension.
Using lb4 repository command, create a repository file. After that, change the inject paramater as below so as to refer to correct data source name.
@inject(datasources.\${AuditDbSourceName}) dataSource: AuditDataSource,
The component exposes a mixin for your repository classes. Just extend your repository class with AuditRepositoryMixin, for all those repositories where you need audit data. See an example below. For a model Group, here we are extending the GroupRepository with AuditRepositoryMixin.
The Actor field is now configurable and can save any string type value in the field.
Though the default value will be userId a developer can save any string field from the current User that is being passed.
The package exposes a conditional mixin for your repository classes. Just extend your repository class with ConditionalAuditRepositoryMixin, for all those repositories where you need audit data based on condition whether ADD_AUDIT_LOG_MIXIN is set true. See an example below. For a model Group, here we are extending the GroupRepository with AuditRepositoryMixin.
Incase you dont have current user binded in your application context and wish to log the activities within your application then in that case you can pass the actor id along with the
options just like
This extension provides support to both juggler (the default loopback ORM) and sequelize.
If your loopback project is already using SequelizeCrudRepository from @loopback/sequelize or equivalent add on repositories from sourceloop packages like SequelizeUserModifyCrudRepository. You'll need to make just two changes:
The import statements should have the suffix /sequelize, like below:
If you've noticed a bug or have a question or have a feature request, search the issue tracker to see if someone else in the community has already created a ticket.
If not, go ahead and make one!
All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can.
If you think this extension is useful, please star it. Appreciation really helps in keeping this project alive.