使用aop进行service层日志打印

@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));
        }
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值