Search code examples
spring-aopaudit-logging

Audit Logs using Spring AOP


We are looking at spring's AOP framework for adding Audit log functionality in our application based on Spring MVC.

In our application whenever we call a service method we pass extra audit log object (in addition to the normal method parameter). This audit log object has few properties pre populated like username, user id and user ip address etc.

In the actual service method we set few more properties on the audit log object depending on method being called or the operation being performed in the service method.

This is very dynamic and varies from method to method.

In the AOP classes we can intercept the method parameters and the return values. But what about the values which are being calculated based on the operation being performed in the service method and then set on the audit log object. This values wont be available in the AOP classes.

Basically we need to populate the audit log object and then save it in the db after execution of the method with the come parameters being set in method being intercepted only.

Is there any we can do this?

Please help.


Solution

  • In the AOP class you should have access to the audit object since you have access to the method parameters. So after the method returns, find the audit object in the method parameters and operate on it. It doesn't matter which argument the audit object is, as long as you can find it by type.

    The values you set on the audit object should still be there since the audit object was passed by reference.