springboot aop 攔截接口執行時間


/**
 * @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;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM