Logging @Aspect
Simplest Logging aspect for @Service and @Controller method. You can also modify it to add more conditions:
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
@Aspect
@Configuration
public class ServiceLoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(ServiceLoggingAspect.class);
@Before("allPublicMethods()")
public void getAllAdvice(JoinPoint joinPoint) {
logger.debug("{} called with args - {}", joinPoint.getSignature().toShortString(), Arrays.toString(joinPoint.getArgs()));
}
@Pointcut("(serviceBeans() || controllerBeans()) && publicMethods()")
private void allPublicMethods() {
// Point cut for all service methods
}
@Pointcut("within(@org.springframework.stereotype.Service *)")
private void serviceBeans() {}
@Pointcut("within(@org.springframework.stereotype.Controller *)")
private void controllerBeans() {}
@Pointcut("execution(public * com.vivek.example..*.*(..))")
private void publicMethods() {}
}
More details: Cheat Sheet
Comments
Post a Comment