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

com.gitee.fufu669.aspect.LogMethodAop Maven / Gradle / Ivy

There is a newer version: 6.666.66021
Show newest version
package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.utils.CacheJsonUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Order(-69)
@SuppressWarnings({"rawtypes"})
/* @author wangfupeng */
public class LogMethodAop {

    public static final Logger logger = LoggerFactory.getLogger(LogMethodAop.class);

    @Around("@annotation(com.gitee.fufu669.aspect.LogMethod)")
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        int logBefore = CacheKeyCommon.DEFAULT_YES_VALUE;
        int logAfter = CacheKeyCommon.DEFAULT_YES_VALUE;
        try {
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            String methodName = proceedingJoinPoint.getSignature().getName();
            Class targetClass = Class.forName(targetName);
            Method[] methods = targetClass.getMethods();
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    logBefore = method.getAnnotation(LogMethod.class).logBefore();
                    logAfter = method.getAnnotation(LogMethod.class).logAfter();
                    break;
                }
            }
        } catch (Exception e) {
            logger.info(e.toString());
        }
        Object target = proceedingJoinPoint.getTarget();
        /* 当前的类的全名*/
        String targetName = target.getClass().getName();
        /* 当前的方法名 */
        String methodName = proceedingJoinPoint.getSignature().getName();
        String logString = "";
        logString += "\n%%%%%%%%%%%%%%%%%%%%%%%%%方法调用前%%%%%%%%%%%%%%%%%%%%%%%%%";
        logString += "\nclass       : " + targetName;
        logString += "\nmethod      : " + methodName;
        Object[] argumentsValues = proceedingJoinPoint.getArgs();
        String parameterString = "\nparameter   : ";
        for (int i = 0; i < argumentsValues.length; i++) {
            Object argumentsValue = argumentsValues[i];
            try {
                parameterString += (i == 0 ? "" : ",") + ("parameter" + (i + 1) + "=" + CacheJsonUtil.toJson(argumentsValue));
            } catch (Exception e) {
                parameterString += (i == 0 ? "" : ",") + ("parameter" + (i + 1) + "=" + "参数无法序列化");
            }
        }
        logString += parameterString;
        logString += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
        if(logBefore==CacheKeyCommon.DEFAULT_YES_VALUE) {
            logger.info(logString);
        }
        logString =  "\n%%%%%%%%%%%%%%%%%%%%%%%%%方法调用后%%%%%%%%%%%%%%%%%%%%%%%%%\n";
        logString += "class       : " + targetName;
        logString += "\nmethod      : " + methodName;
        logString += parameterString + "";
        Object value = proceedingJoinPoint.proceed();
        try {
            logString += ("\nreturn class: " + value.getClass());
        } catch (Exception e) {
            logString += ("\nreturn class: " + "结果没有类名");
        }
        try {
            logString += ("\nreturn value: " + CacheJsonUtil.toJson(value));
        } catch (Exception e) {
            logString += ("\nreturn value: " + "结果无法序列化" + value);
        }
        logString += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%";
        if(logAfter==CacheKeyCommon.DEFAULT_YES_VALUE) {
            logger.info(logString);
        }
        return value;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy