import org.springframework.boot.logging.LogLevel;
import java.lang.annotation.*;
/**
* 添加该注解,则添加该注解的方法的入参或者出参会自动打印
*
* @author shizi
* @since 2019/12/24 9:56 下午
*/
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface EnableAopLog {
/**
* 是否打印请求
* @return 默认不打印
*/
boolean printRequest() default false;
/**
* 如果打印的话,则打印的级别是什么
* @return 默认为debug
*/
LogLevel logLevel() default LogLevel.DEBUG;
/**
* 是否打印返回值
* @return 默认不打印
*/
boolean printResponse() default false;
}
解析部分:
import com.alibaba.fastjson.JSON;
import com.isyscore.iop.panda.exception.BusinessException;
import com.isyscore.isc.neo.util.TimeRangeStrUtil;
import lombok.extern.slf4j.Slf4j;
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.springframework.boot.logging.LogLevel;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.HashMap;
import static com.isyscore.iop.panda.constant.AppConstant.LOG_PRE;
/**
* @author robot
*/
@Slf4j
@Aspect
@Component
public class ControllerAop {
/**
* 拦截方法中添加注解{@link EnableAopLog}的类和方法
*/
@Around("@annotation(com.isyscore.iop.panda.annotation.EnableAopLog) || @within(com.isyscore.iop.panda.annotation.EnableAopLog)")
public Object aroundParamFun1(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
HashMap<String, Object> outInfo = new HashMap<>();
Signature sig = pjp.getSignature();
MethodSignature methodSignature = (MethodSignature) sig;
Method currentMethod;
try {
currentMethod = pjp.getTarget().getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
} catch (NoSuchMethodException e) {
throw new BusinessException(e);
}
EnableAopLog enableAopLog = null;
if (currentMethod.getDeclaringClass().isAnnotationPresent(EnableAopLog.class)) {
enableAopLog = currentMethod.getDeclaringClass().getAnnotation(EnableAopLog.class);
}
if (currentMethod.isAnnotationPresent(EnableAopLog.class)) {
enableAopLog = currentMethod.getAnnotation(EnableAopLog.class);
}
Object result = null;
try {
result = pjp.proceed();
if (null == enableAopLog) {
return result;
}
if (enableAopLog.printResponse()) {
outInfo.put("response", result);
}
} catch (Throwable e) {
outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
throw e;
}
if (enableAopLog.printRequest()) {
outInfo.put("fun", currentMethod.getName());
outInfo.put("parameters", pjp.getArgs());
}
if (enableAopLog.printResponse()) {
outInfo.put("timeout", TimeRangeStrUtil.parseTime(System.currentTimeMillis() - start));
}
LogLevel logLevel = enableAopLog.logLevel();
switch (logLevel) {
case OFF:
break;
case DEBUG:
log.debug(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
break;
case TRACE:
log.trace(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
break;
case INFO:
log.info(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
break;
case WARN:
log.warn(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
break;
case ERROR:
log.error(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
break;
case FATAL:
log.error(LOG_PRE + " 请求:" + JSON.toJSONString(outInfo));
break;
default:
break;
}
return result;
}
}