1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.qingchen.api.aspect;
import com.alibaba.nacos.shaded.com.google.common.base.Stopwatch; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch;
@Component @Slf4j @Aspect public class ServiceLogAspect { @Around("execution(* com.qingchen.service.impl..*.*(..))") public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {
StopWatch stopwatch=new StopWatch(); String pointName=joinPoint.getTarget().getClass().getName()+"."+joinPoint.getSignature().getName(); stopwatch.start(pointName+":业务执行开始");
Object proceed=joinPoint.proceed(); stopwatch.stop(); log.info(stopwatch.prettyPrint()); log.info(stopwatch.shortSummary()); log.info("任务总数"+stopwatch.getTaskCount()); log.info("执行总时间: "+stopwatch.getTotalTimeMillis());
long takeTimes=stopwatch.getTotalTimeMillis(); if(takeTimes>3000){ log.error("执行位置{},耗费了{}毫秒",pointName,takeTimes); }else if(takeTimes>2000){ log.warn("执行位置{},执行时间稍微有点长,耗费了{}毫秒",pointName,takeTimes); }else{ log.info("执行位置{},执行时间正常,耗费了{}毫秒",pointName,takeTimes); }
return proceed; } }
|