LogAspect.java 2.18 KB
package com.glsx.base.aop;

import com.glsx.exception.ApiException;
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;

@Component
@Aspect
@Slf4j
public class LogAspect {
    /**
     * 切点 注解拦截
     */
    @Pointcut("@annotation(com.glsx.base.aop.SysLog)")
    public void methodAspect() {

    }

    /**
     * @param point
     * @return
     * @throws Throwable
     * @Title:doAround
     * @Description: 环绕触发
     * @author fengzhi
     * @date 2020年12月22日
     */
    @Around("methodAspect()")
    public Object doAround(ProceedingJoinPoint point)  {
        //获取类名
        String className = point.getTarget().getClass().getName();

        //获取方法名
        MethodSignature signature = (MethodSignature) point.getSignature();
        String methodName = signature.getMethod().getName();

        //访问目标方法的参数名和参数值
        Object[] paramVals = point.getArgs();


        //todo 打印接口的入参
        /*//stream转为List
        String[] paramNames = ((CodeSignature) point.getSignature()).getParameterNames();
        List<Object> listValues = Arrays.stream(paramVals).collect(Collectors.toList());
        List<String> listNames = Arrays.stream(paramNames).collect(Collectors.toList());
        //将2个分为为key,value的list转为map,打印参数对象
        Map<String, Object> params = MapUtils.listToMap(listNames, listValues);*/

        log.info(className + "===调用" + methodName + "开始===");

        //用改变后的参数执行目标方法
        Object returnValue = null;
        try {
            returnValue = point.proceed(paramVals);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new ApiException(className+"调用"+methodName+"失败,message:"+throwable.getMessage());
        }

        log.info(className + "===" + methodName + "返回===" + returnValue);
        return returnValue;
    }


}