All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.coufran.springboot.starter.log.AutoLogAdvice Maven / Gradle / Ivy

The newest version!
package cn.coufran.springboot.starter.log;

import cn.coufran.springboot.starter.log.logger.Logger;
import cn.coufran.springboot.starter.log.serializer.Serializer;
import cn.coufran.springboot.starter.log.serializer.SerializerFactory;
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.reflect.MethodSignature;
import org.slf4j.event.Level;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 日志切面
 * @author Coufran
 * @version 1.1.0
 * @since 1.0.0
 */
@Component
@Aspect
public class AutoLogAdvice {
    /** 日志记录器 */
    private Logger logger;
    /** 序列化器工厂 */
    private SerializerFactory serializerFactory;
    /** 业务异常类 */
    private Collection> serviceExceptionClasses = new ArrayList<>();

    /**
     * 设置日志记录器
     * @param logger 日志记录器
     */
    @Resource
    public void setLogger(Logger logger) {
        this.logger = logger;
    }

    /**
     * 设置序列化器工厂
     * @param serializerFactory 序列化器工厂
     */
    @Resource
    public void setSerializerFactory(SerializerFactory serializerFactory) {
        this.serializerFactory = serializerFactory;
    }

    /**
     * 设置业务异常类
     * @param serviceExceptionClasses 业务异常类
     */
    @Autowired(required = false)
    public void setServiceExceptionClasses(Collection> serviceExceptionClasses) {
        this.serviceExceptionClasses.addAll(serviceExceptionClasses);
    }

    /**
     * 添加业务异常类
     * @param serviceExceptionClasses 业务异常类
     */
    public void addServiceExceptionClass(Class... serviceExceptionClasses) {
        this.serviceExceptionClasses.addAll(Arrays.asList(serviceExceptionClasses));
    }

    /**
     * 切面逻辑
     * @param pjp pjp
     * @return result
     * @throws Throwable error
     */
    @Around("@annotation(cn.coufran.springboot.starter.log.AutoLog)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        AutoLog resultAutoLog = null; // 默认结果标记
        List parametersAutoLog = null; // 默认参数标记

        // 获取注解标记
        Signature signature = pjp.getSignature();
        if (signature instanceof MethodSignature) {
            MethodSignature methodSignature = (MethodSignature) signature;
            final AutoLog methodAutoLog = methodSignature.getMethod().getAnnotation(AutoLog.class);
            resultAutoLog = methodAutoLog;
            Parameter[] parameters = methodSignature.getMethod().getParameters();
            parametersAutoLog = Arrays.stream(parameters)
                    .map(parameter -> parameter.getAnnotation(AutoLog.class))
                    .map(autoLog -> autoLog != null ? autoLog : methodAutoLog)
                    .collect(Collectors.toList());
        }

        // 打印参数
        Level logLevel = resultAutoLog.level();
        String interfaceId = null;
        try {
            if (logger.isLogEnabled(logLevel)) {
                Object[] args = pjp.getArgs();
                for (int i=0; i serviceExceptionClass : serviceExceptionClasses) {
            if (serviceExceptionClass.isInstance(e)) {
                return true;
            }
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy