全局日志处理【出参,入参等】

话不多说,直接上Code

package com.xxx.customAnnotations;

import java.lang.annotation.*;

/**
 * 日志处理(结合SysLogAspect.java使用,作用于方法上,自动记录方法的入参、出参等信息)
 *
 * @author : She Leiming
 * @date : 2019-08-20 14:48
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
}
package com.xxx.customAnnotations;

import com.alibaba.fastjson.JSON;
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.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 日志记录切面(入参,出参,批次,耗时)
 *
 * @author : She Leiming
 * @date : 2019-08-20 14:49
 */
@Slf4j
@Aspect
@Component
public class SysLogAspect {

    /**
     * 方法添加注解模式
     */
    @Pointcut("@annotation(com.xxx.customAnnotations.SysLog)")
    public void log() {
    }

    /**
     * 扫包模式(仅扫Controller)
     */
    @Pointcut("within(*com.xxx.controller.*.*)")
    public void scanerLog() {
    }

    /**
     * 自动记录方法日志(扫包模式)
     *
     * @param joinPoint 切入点
     * @return java.lang.Object 响应信息
     *
     * @author : She Leiming
     * @date : 2019-08-20 15:02:35
     */
    @Around(value = "scanerLog()")
    public Object scanerLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();

        String className = joinPoint.getTarget().getClass().getName();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = SingleLocalVariableTableParameterNameDiscoverer.getInstance().getParameterNames(method);
        Object[] requestParams = joinPoint.getArgs();
        if (null != parameterNames) {
            for (int i = 0; i < parameterNames.length; i++) {
                requestParams[i] = parameterNames[i] + " = " + requestParams[i];
            }
        }
        long batch = System.currentTimeMillis();

        log.info(":::::: ==> " + className + "." + method.getName() + "()");
        log.info(":::::: ==> Requeat Batch  : " + batch);
        log.info(":::::: ==> Requeat Params : " + JSON.toJSONString(requestParams));

        Object responseInfo = joinPoint.proceed();

        long end = System.currentTimeMillis();

        log.info(":::::: <== " + className + "." + method.getName() + "()");
        log.info(":::::: <== Requeat  Batch : " + batch);
        log.info(":::::: <== Time Consuming : " + (end - start) + " ms");
        log.info(":::::: <== Response  Info : " + JSON.toJSONString(responseInfo));

        return responseInfo;
    }

    /**
     * 加入注解自动记录方法日志(方法添加注解模式)
     *
     * @param joinPoint 切入点
     * @return java.lang.Object 响应信息
     *
     * @author : She Leiming
     * @date : 2019-08-20 17:29:03
     */
    @Around(value = "log()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();

        String className = joinPoint.getTarget().getClass().getName();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = SingleLocalVariableTableParameterNameDiscoverer.getInstance().getParameterNames(method);
        Object[] requestParams = joinPoint.getArgs();
        if (null != parameterNames) {
            for (int i = 0; i < parameterNames.length; i++) {
                requestParams[i] = parameterNames[i] + " = " + requestParams[i];
            }
        }
        long batch = System.currentTimeMillis();

        log.info(":::::: ==> " + className + "." + method.getName() + "()");
        log.info(":::::: ==> Requeat Batch  : " + batch);
        log.info(":::::: ==> Requeat Params : " + JSON.toJSONString(requestParams));

        Object responseInfo = joinPoint.proceed();

        long end = System.currentTimeMillis();

        log.info(":::::: <== " + className + "." + method.getName() + "()");
        log.info(":::::: <== Requeat  Batch : " + batch);
        log.info(":::::: <== Time Consuming : " + (end - start) + " ms");
        log.info(":::::: <== Response  Info : " + JSON.toJSONString(responseInfo));

        return responseInfo;
    }
}

/**
 * 用于获取方法参数名所使用到的对象
 *
 * @author : She Leiming
 * @date : 2019-08-20 14:49
 */
class SingleLocalVariableTableParameterNameDiscoverer {
    private static volatile LocalVariableTableParameterNameDiscoverer instance = null;

    private SingleLocalVariableTableParameterNameDiscoverer() {
    }

    public static synchronized LocalVariableTableParameterNameDiscoverer getInstance() {
        if (instance == null) {
            instance = new LocalVariableTableParameterNameDiscoverer();
        }
        return instance;
    }
}

你可能感兴趣的:(Java,SpringBoot,AOP)