@Slf4j
@Aspect
@Configuration
public class ServiceLogAspectConfig {
@Pointcut("within(com.yuxiang.intelligence.service.service.base.IBaseService+)")
public void serviceLog() {
}
@Around("serviceLog()")
public Object doAroundService(ProceedingJoinPoint proceedingJoinPoint) {
try {
Instant instStart = Instant.now();
Object object = proceedingJoinPoint.proceed();
Instant instEnd = Instant.now();
long timeDifference = Duration.between(instStart, instEnd).toMillis();
BigDecimal divideUnsigned = BigDecimal.valueOf(timeDifference)
.divide(BigDecimal.valueOf(1000L), new MathContext(2));
log.info("-------------------start {}-----------------", Thread.currentThread().getName());
log.info("执行class_method={}, args={}",
proceedingJoinPoint.getSignature().getDeclaringTypeName() + "." + proceedingJoinPoint
.getSignature().getName(), proceedingJoinPoint.getArgs());
log.info("{}请求耗时={}秒", Thread.currentThread().getName(), divideUnsigned);
log.info("-------------------{} end-----------------", Thread.currentThread().getName());
return object;
} catch (Throwable throwable) {
log.error("发现异常:class_method={}, args={}, 异常信息:{}", proceedingJoinPoint
.getSignature().getDeclaringTypeName() + "." + proceedingJoinPoint.getSignature().getName(),
proceedingJoinPoint.getArgs(), throwable);
return CommonResult
.buildFailure(HttpStatus.INTERNAL_SERVER_ERROR.value(), String.valueOf(throwable));
}
}
}
使用aop进行service层日志打印
最新推荐文章于 2022-09-17 14:54:25 发布