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

top.doudou.common.aop.AopLogProcessor Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.aop;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.doudou.common.aop.collector.LogCollectorExecutor;
import top.doudou.common.tool.aspect.AspectUtils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @Description 切面处理器
 * @Author 傻男人 <[email protected]>
 * @Date 2020-09-24 14:53
 * @Version V1.0
 */
@Component
public class AopLogProcessor {

    private final LogCollectorExecutor logCollectorExecutor;


    public AopLogProcessor(@Autowired LogCollectorExecutor logCollectorExecutor) {
        this.logCollectorExecutor = logCollectorExecutor;
    }

    /**
     * 处理 日志数据切面
     *
     * @param data  日志数据
     * @param point 切入point对象
     * @return 返回执行结果
     * @throws Throwable Exceptions in AOP should be thrown out and left to the specific business to handle
     */
    public Object proceed(LogData data, ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        AopLog aopLog = signature.getMethod().getAnnotation(AopLog.class);
        ApiOperation apiOperation = signature.getMethod().getAnnotation(ApiOperation.class);
        if(null != apiOperation){
            data.setInterfaceName(apiOperation.value());
        }
        if (aopLog == null) aopLog = point.getTarget().getClass().getAnnotation(AopLog.class);
        if (aopLog != null) {
            if (!aopLog.logOnErr()) {
                logProcessBefore(aopLog, data, point);
            }
            return proceed(aopLog, data, point);
        }
        return point.proceed();
    }


    /**
     * 执行前记录 app应用信息 http等信息
     *
     * @param aopLog 注解对象
     * @param data   日志数据
     * @param point  切入point对象
     */
    public void logProcessBefore(AopLog aopLog, LogData data, ProceedingJoinPoint point) {
        MethodSignature signature = (MethodSignature) point.getSignature();
        data.setType(aopLog.type());
        String interfaceName = aopLog.interfaceName();
        if(StringUtils.isNotBlank(interfaceName)){
            data.setInterfaceName(interfaceName);
        }
        data.setMethod(signature.getDeclaringTypeName() + "#" + signature.getName());
        LogDataExtractor.logHttpRequest(data, aopLog.headers());
        if (aopLog.args()) {
            data.setArgs(AspectUtils.getParametersToStr(point));
        }
    }

    /**
     * 方法执行处理记录
     *
     * @param aopLog 注解对象
     * @param data   日志数据
     * @param point  切入point对象
     * @return 返回执行结果
     * @throws Throwable Exceptions in AOP should be thrown out and left to the specific business to handle
     */
    private Object proceed(AopLog aopLog, LogData data, ProceedingJoinPoint point) throws Throwable {
        try {
            Object result = point.proceed();
            if (aopLog.respBody()) {
                data.setRespond(LogDataExtractor.getResult(result));
            }
            data.setSuccess(true);
            return result;
        } catch (Throwable throwable) {
            if (aopLog.logOnErr()) {
                logProcessBefore(aopLog, data, point);
            }
            data.setSuccess(false);
            if (aopLog.stackTraceOnErr()) {
                try (StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw, true)) {
                    throwable.printStackTrace(writer);
                    LogData.step("Fail : \n" + sw.toString());
                }
            }
            throw throwable;
        } finally {
            data.toCostTime();
            LogData.setCurrent(data);
            if (!aopLog.logOnErr() || (aopLog.logOnErr() && !data.isSuccess())) {
                if (aopLog.asyncMode()) {
                    logCollectorExecutor.asyncExecute(aopLog.collector(), data);
                } else {
                    logCollectorExecutor.execute(aopLog.collector(), data);
                }
            }
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy