AOP日志

考慮單獨(dú)的對(duì)每個(gè)方法做日志輸出輸入以及校驗(yàn)太麻煩了,就寫了個(gè)aop去針對(duì)每個(gè)特定包下的內(nèi)容進(jìn)行處理,代碼如下

package com.gaara.aop;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
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;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Arrays;

/********************************
 *    Author Gaara              *
 *    Version 1.0               *
 *    @ClassName RestLogAspect
 *    @Date 2021/4/25 上午11:00     
 *    @Description TODO         *
 ********************************/
@Slf4j
@Aspect
@Component
@AllArgsConstructor
public class RestLogAspect {

    private HttpServletRequest request;

    // 攔截包中定義的方法 不包含子包中的方法
    @Pointcut("execution(public * com.gaara.controller.*.*(..))")
    public void controllerPointCut(){}

    // 攔截包中定義的方法,含子包
    @Pointcut("execution(public * com.gaara.service..*.*(..))")
    public void servicePointCut(){}

    @Around("controllerPointCut()")
    public Object controllerAround(ProceedingJoinPoint point)throws Throwable{
        long t = System.currentTimeMillis();
        Object ret;
        try{
            ret = point.proceed();
            if (null == ret){
                ret = "";
            }
            log.info("{} => {} => {}",request.getMethod(),request.getRequestURL().toString(), Arrays.toString(point.getArgs()));
            log.info("耗時(shí) == {}ms",(System.currentTimeMillis() - t));
        }catch (Throwable e){
            log.warn("{} => {} => {}",request.getMethod(),request.getRequestURL().toString(), e.getMessage());
            log.warn("耗時(shí) == {}ms",(System.currentTimeMillis() - t));
            throw e;
        }
        return ret;
    }

    @Around("servicePointCut()")
    public Object serviceAround(ProceedingJoinPoint point)throws Throwable{
        try {
            Signature signature = point.getSignature();
            if (!(signature instanceof MethodSignature)){
                return point.proceed();
            }

            // 獲取基本信息
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            Class<?> clazz = method.getDeclaringClass();
            Object[] args = point.getArgs();
            String methodInfo = clazz.getName()+"#"+method.getName();


            boolean b = !methodInfo.contains("#autoJoinClose") &&
                    !methodInfo.contains("#resetAndSendByState")&&
                    !methodInfo.contains("#xxx");

            if (b){
                // 入?yún)?                String params = null;
                try {
                    params = JSONObject.toJSONStringWithDateFormat(args,"yyyy-MM-dd HH-mm-ss");
                }catch (Exception e){
                    log.warn(e.getMessage());
                }
                log.info("開始方法【{}】調(diào)用,入?yún)椋骸緖}】",methodInfo,params);
            }

            Object proceed = point.proceed();

            //出參
            String returnValue = null;
            try {
                returnValue = JSONObject.toJSONStringWithDateFormat(proceed,"yyyy-MM-dd HH-mm-ss");
            }catch (Exception e){
                log.warn(e.getMessage());
            }
            if (b){
                log.info("【{}】方法結(jié)束調(diào)用,出參為:【{}】",methodInfo,returnValue);
            }
            return proceed;

        }catch (Throwable e){
            log.error(e.toString());
            throw e;
        }
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容