/** * @description: 記錄接口執行時間日志的記錄 * @author: * @create 2018-12-27 16:32 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface OptimizeLog { }
package com.wsh.b2q.pc.aspectj; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; /** * @description: * @author: lvws * @create 2018-12-27 16:34 */ @Aspect @Component @Slf4j public class OptimizeLogAspect { @Pointcut("@annotation(OptimizeLog的路徑)") public void logPointCut() { } @Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { //開始時間 long start = System.currentTimeMillis(); //執行方法 Object result = point.proceed(); long end = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) point.getSignature(); //請求的方法名 String className = point.getTarget().getClass().getName(); String methodName = signature.getName(); log.debug("【接口執行時間】接口名:{}.{},執行時間:{}毫秒",className,methodName,(end-start)); log.info("【接口執行時間】接口名:{}.{},執行時間:{}毫秒",className,methodName,(end-start)); return result; } }