I'm building a Spring web application. Using Spring AOP's aspect to do audit logging.
I've got business class with methods like the following and i want to audit log in certain cases
- during method execution
after method returns
@Aspect public class MyBusinessClass {... public void registerUser() { // do some business logic here... userId = // userService.saveUser(...).getId(); // etc... if(successful) { // then send Email notification audit.log(userId, "success sending email notification"... some other params); else { audit.log(userId, "fail to send email notification"...); } //do some more logic before returning method audit.log(userId,"successfully registered user...." ...); ..// method returns }
I've already got Aspects defined in a separate class.
I'm having problems because all the common simple examples i've seen on here and on other articles seemed to be very simple. For example, they work easily because the most complicated i've seen only go as far as extracting method params into the aspect's advice..
But my problem here is that i wan't to somehow not just extract local variables into the Aspect's advice but do the audit logging more than once and also based on certain conditions.
Don't get me wrong, we've already got Log4j logging to log to file in the same places but we also require to do these audit logging to DB too.
I just want to know what's the solution to achieve what i want to do in a high level.
Apologies for the slightly poor code formatting... (it's mostly pseudocode). I just can't get this code formatting to work on StackOverflow. It's much easier on Github markdown.