Several Spring AOP https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html ready to use aspects.
pom.xml:
<dependency>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>digitalcollections-commons-springaop</artifactId>
<version>{set to current version}</version>
</dependency>
Aspect oriented logging around methods:
- logs method call with method parameters (if not null), e.g.
14:16:15.960 [qtp267098351-23] INFO de.example.portal.frontend.webapp.controller.NewsController - viewNews(model=BindingAwareModelMap {menu=news, starsign=Stier, names_of_day=[Ljava.l...)
- logs duration (in ms) of method execution (only if logging is at minimum at debug level), e.g.
14:16:15.986 [qtp267098351-23] DEBUG de.example.portal.frontend.webapp.controller.NewsController - viewNews(): duration 26 ms
Extend AbstractAopMethodLogger with your project specific AopMethodLogger and define the method pointcuts to be logged. Example:
package de.example.portal.frontend.webapp.aop;
import de.digitalcollections.commons.springaop.AbstractAopMethodLogger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AopMethodLogger extends AbstractAopMethodLogger {
@Pointcut("execution(public * de.example.portal.frontend.webapp.controller..*Controller.*(..)) || "
+ "execution(public * de.example.portal.business.service..*Service.*(..)) || "
+ "execution(public * de.example.portal.backend.repository..*.*Repository.*(..))")
@Override
public void methodsToBeLogged() {
}
}
Add package containing your implementation to ComponentScan of your Spring config and add @EnableAspectJAutoProxy annotation. Example:
@Configuration
@ComponentScan(basePackages = {
"de.example.portal.frontend.webapp.aop",
...
})
@EnableAspectJAutoProxy
...
public class SpringConfigWeb extends WebMvcConfigurerAdapter {
...
}