Keep a record of what users are doing in Spring MVC

I have a very large application written in Spring MVC. I want to keep an “activity record” that tracks in the database what users are doing in my application.

At the first stage, I just want an activity log, it may just be a list of controller methods called during user actions, but later I would like this information to be more “readable”, i.e. instead of "modifyAccount (accountId = 5, accountBalance = 500) something like" user X updates the balance for account 5 to 500. "

The problem that I see is that since my application is very large, I would not want to modify each of my actions to add this logging mechanism. Is there a more flexible, declarative way to do this?

+4
source share
1 answer

You can use Aspect Oriented Programming (AOP) to automate logging.

http://static.springsource.org/spring/docs/2.0.8/reference/aop.html

The page above shows many examples of using AOP with spring. One example is using annotations to find methods of interest to you. Using this annotation is an easy way to determine which methods should be logged.

Annotations @Audit

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Auditable { ... } 

Audit method

 @After("@annotation(auditable)", argNames="joinPoint") public void audit(JoinPoint joinPoint) { logger.info("Called {} with arguments {}", joinPoint.getSignature().getLongString(), joinPoint.getArgs()); } 

I have not tested this code, but it got the point.

+8
source

Source: https://habr.com/ru/post/1495263/


All Articles