其他分享
首页 > 其他分享> > util工具类---aop 日志打印

util工具类---aop 日志打印

作者:互联网


import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
@Slf4j
public class ControllerLogAspect {

    private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    @Pointcut("execution(public * in.cash.financial.*.*.controller..*.*(..)) ")
    public void webLog() {

    }


    @Before("webLog()")
    public void before(JoinPoint joinPoint){
        Object args[] = joinPoint.getArgs();
        MethodSignature sig = (MethodSignature)joinPoint.getSignature();
        Method method = sig.getMethod();
        if(null != method.getDeclaringClass().getName() && null != method.getName() && null != args && args.length > 0){
            log.info(">>>>>>>>>请求链路开始{}, 请求方法:{}, 请求参数:{}",method.getDeclaringClass().getName(),method.getName(), StringUtils.join(args," : "));
        }
    }

    @AfterReturning(value="webLog()",returning="rvt")
    public void after(JoinPoint joinPoint,Object rvt){
        MethodSignature sig1 = (MethodSignature)joinPoint.getSignature();
        Method method1 = sig1.getMethod();
        if(null != rvt && null != method1.getDeclaringClass()){
            try{
                log.info(">>>>>>>>>>>>>>结束:{}, 请求方法:{}, 接口返回数据:{}",method1.getDeclaringClass().getName(),method1.getName(),new Gson().toJson(rvt));
            }catch (Exception e){

            }
        }
    }


    @AfterThrowing(pointcut = "webLog()", throwing = "e")
    public void  doExceptionPrint(JoinPoint joinPoint, Throwable e){
        log.error("异常信息=",e);
    }


}

标签:util,getName,joinPoint,method,---,aop,import,MethodSignature,public
来源: https://blog.csdn.net/ywj776199845/article/details/120335791