Вы можете создать аспект, который регистрирует журналы входа / выхода и время выполнения для каждого метода данных пакетов.
Для расчета времени выполнения вы можете использовать пружину Stopwatch
. Тем не менее, вы должны быть осторожны с влиянием производительности. (Этот класс не рекомендуется для производственной среды)
import org.springframework.util.StopWatch;
@Aspect
@Component
public class LoggingAspect {
private final Logger log = LoggerFactory.getLogger(this.getClass());
/**
* Pointcut that matches all services and Web REST endpoints.
*/
@Pointcut("within(@org.springframework.stereotype.Service *)" +
" || within(@org.springframework.web.bind.annotation.RestController *)")
public void springBeanPointcut() {
// Method is empty as this is just a Pointcut, the implementations are in the advices.
}
/**
* Pointcut that matches all Spring beans in the application's endpoint packages.
*/
@Pointcut("within(your.pack.num1..*)" +
" || within(your.pack.num2..*)" +
" || within(your.pack.num3..*)")
public void applicationPackagePointcut() {
// Method is empty as this is just a Pointcut, the implementations are in the advices.
}
/**
* Advice that logs methods throwing exceptions.
*
* @param joinPoint join point for advice
* @param e exception
*/
@AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL");
}
/**
* Advice that logs when a method is entered and exited.
*
* @param joinPoint join point for advice
* @return result
* @throws Throwable throws IllegalArgumentException
*/
@Around("applicationPackagePointcut() && springBeanPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
StopWatch stopWatch;
if (log.isDebugEnabled()) {
stopWatch= new StopWatch();
stopWatch.start();
log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
}
try {
try{
Object result = joinPoint.proceed();
}finally{
stopWatch.stop();
}
if (log.isDebugEnabled()) {
log.debug("Exit: {}.{}() with result = {} and execution time {}", joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(), result,stopWatch.getTotalTimeMillis());
}
return result;
} catch (IllegalArgumentException e) {
log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()),
joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
throw e;
}
}
}