LogAspect.java
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.glsx.base.aop;
import com.glsx.exception.ApiException;
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.stereotype.Component;
@Component
@Aspect
@Slf4j
public class LogAspect {
/**
* 切点 注解拦截
*/
@Pointcut("@annotation(com.glsx.base.aop.SysLog)")
public void methodAspect() {
}
/**
* @param point
* @return
* @throws Throwable
* @Title:doAround
* @Description: 环绕触发
* @author fengzhi
* @date 2020年12月22日
*/
@Around("methodAspect()")
public Object doAround(ProceedingJoinPoint point) {
//获取类名
String className = point.getTarget().getClass().getName();
//获取方法名
MethodSignature signature = (MethodSignature) point.getSignature();
String methodName = signature.getMethod().getName();
//访问目标方法的参数名和参数值
Object[] paramVals = point.getArgs();
//todo 打印接口的入参
/*//stream转为List
String[] paramNames = ((CodeSignature) point.getSignature()).getParameterNames();
List<Object> listValues = Arrays.stream(paramVals).collect(Collectors.toList());
List<String> listNames = Arrays.stream(paramNames).collect(Collectors.toList());
//将2个分为为key,value的list转为map,打印参数对象
Map<String, Object> params = MapUtils.listToMap(listNames, listValues);*/
log.info(className + "===调用" + methodName + "开始===");
//用改变后的参数执行目标方法
Object returnValue = null;
try {
returnValue = point.proceed(paramVals);
} catch (Throwable throwable) {
throwable.printStackTrace();
throw new ApiException(className+"调用"+methodName+"失败,message:"+throwable.getMessage());
}
log.info(className + "===" + methodName + "返回===" + returnValue);
return returnValue;
}
}