19
One common requirement in developing enterprise applications is to ensure audit logs are available for data security and traceability. Who made the changes, when and where. Such requirement is not only dictated by corporate IT policies but is also required by government laws. Considering most enterprise applications have at least 50 domain objects, implementing audit logs in each of them can be time consuming. So, a generic solution must be established to minimize coding when creating audit logs.
The Solution
- Use Hibernate interceptors to trigger change events.
- Use Java reflections to retrieve old and new data.
- Create an interface to switch logging on or off for every data object.
- Create an Auditable interface as a marker on data that needs to be audited. Any data object that needs to be audited must implement Auditable interface.
- Create an AuditLog to store log information. This contains the date (when), user (who), class name, object id and the audit message (what).
- Create an AuditLogInterceptor that will keep track of all data change events and store all changes performed.
- Create a utility that can retrieve object values using Java reflections to process generic data retrieval.
- Create sample usage of auditing.









