Skip to content

Commit 58069a9

Browse files
committed
complete enable log use custom pointcut exp 100%
1 parent fcaf5f0 commit 58069a9

File tree

5 files changed

+128
-20
lines changed

5 files changed

+128
-20
lines changed

quick-log/src/main/java/com/quick/log/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@SpringBootApplication
1717
@EnableScheduling
1818
@QsEnableGlobalExceptionHandler
19-
@QsEnableAroundLog
19+
@QsEnableAroundLog(value = "execution(public * com.*..service.*.*(..))")
2020
public class Application {
2121
public static void main(String[] args) {
2222
SpringApplication.run(Application.class, args);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.quick.component.config.logAspect;
2+
3+
import cn.hutool.core.util.StrUtil;
4+
import cn.hutool.core.util.URLUtil;
5+
import cn.hutool.json.JSONUtil;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.aopalliance.intercept.MethodInterceptor;
8+
import org.aopalliance.intercept.MethodInvocation;
9+
import org.springframework.util.StopWatch;
10+
import org.springframework.util.StringUtils;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
import org.springframework.web.context.request.RequestContextHolder;
14+
import org.springframework.web.context.request.ServletRequestAttributes;
15+
16+
import javax.servlet.http.HttpServletRequest;
17+
import java.lang.reflect.Method;
18+
import java.lang.reflect.Parameter;
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
@Slf4j
25+
public class LogAdvice implements MethodInterceptor {
26+
27+
@Override
28+
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
29+
30+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
31+
HttpServletRequest request = attributes.getRequest();
32+
//记录请求信息
33+
StopWatch stopWatch = new StopWatch();
34+
stopWatch.start();
35+
Object result = methodInvocation.proceed();
36+
stopWatch.stop();
37+
Method method = methodInvocation.getMethod();
38+
String urlStr = request.getRequestURL().toString();
39+
log.info("===================================begin==================================================");
40+
log.info("req path: {}", StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
41+
log.info("req ip: {}", request.getRemoteUser());
42+
log.info("req method: {}", request.getMethod());
43+
log.info("req params: {}", getParameter(method, methodInvocation.getArguments()));
44+
log.info("req result: {}", JSONUtil.toJsonStr(result));
45+
log.info("req uri: {}", request.getRequestURI());
46+
log.info("req url: {}", request.getRequestURL().toString());
47+
log.info("req cost: {}ms", stopWatch.getLastTaskTimeMillis());
48+
log.info("===================================end===================================================");
49+
return result;
50+
}
51+
52+
private Object getParameter(Method method, Object[] args) {
53+
List<Object> argList = new ArrayList<>();
54+
Parameter[] parameters = method.getParameters();
55+
for (int i = 0; i < parameters.length; i++) {
56+
//将RequestBody注解修饰的参数作为请求参数
57+
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
58+
if (requestBody != null) {
59+
argList.add(args[i]);
60+
}
61+
//将RequestParam注解修饰的参数作为请求参数
62+
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
63+
if (requestParam != null) {
64+
Map<String, Object> map = new HashMap<>();
65+
String key = parameters[i].getName();
66+
if (!StringUtils.isEmpty(requestParam.value())) {
67+
key = requestParam.value();
68+
}
69+
map.put(key, args[i]);
70+
argList.add(map);
71+
}
72+
}
73+
if (argList.size() == 0) {
74+
return null;
75+
} else if (argList.size() == 1) {
76+
return argList.get(0);
77+
} else {
78+
return argList;
79+
}
80+
}
81+
82+
83+
}

quick-platform-component/src/main/java/com/quick/component/config/WebLogAspect.java renamed to quick-platform-component/src/main/java/com/quick/component/config/logAspect/WebLogAspect.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.quick.component.config;
1+
package com.quick.component.config.logAspect;
22

33
import cn.hutool.core.util.StrUtil;
44
import cn.hutool.core.util.URLUtil;
@@ -24,25 +24,10 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27-
@Aspect
27+
//@Aspect
2828
@Slf4j
2929
public class WebLogAspect {
3030

31-
// @Pointcut("execution(public * com.quick.log..controller.*.*(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数
32-
// @Pointcut("${pointcut.value}")//两个..代表所有子目录,最后括号里的两个..代表所有参数
33-
// @Pointcut("#{@value}")//两个..代表所有子目录,最后括号里的两个..代表所有参数
34-
// public void logPointCut() {
35-
// }
36-
//
37-
// @Before("logPointCut()")
38-
// public void doBefore(JoinPoint joinPoint) throws Throwable {
39-
// }
40-
//
41-
// @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的参数名一致
42-
// public void doAfterReturning(Object ret) throws Throwable {
43-
// }
44-
45-
// @Around("logPointCut()")
4631
@Around("execution(public * com..controller.*.*(..))")
4732
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
4833
//获取当前请求对象
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.quick.component.config.logAspect;
2+
3+
import com.quick.component.enables.QsEnableAroundLog;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
6+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
7+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
8+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
9+
import org.springframework.core.annotation.AnnotationAttributes;
10+
import org.springframework.core.type.AnnotationMetadata;
11+
12+
import java.util.function.Supplier;
13+
14+
/**
15+
* 动态设置切点表达式
16+
*/
17+
@Slf4j
18+
public class WebLogAspectConfig implements ImportBeanDefinitionRegistrar {
19+
20+
21+
@Override
22+
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
23+
AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(QsEnableAroundLog.class.getName()));
24+
String value = annoAttrs.getString("value");
25+
26+
log.info("init log aspect: {}", value);
27+
BeanDefinitionBuilder beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(AspectJExpressionPointcutAdvisor.class, new Supplier<AspectJExpressionPointcutAdvisor>() {
28+
@Override
29+
public AspectJExpressionPointcutAdvisor get() {
30+
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
31+
advisor.setExpression(value);
32+
advisor.setAdvice(new LogAdvice());
33+
return advisor;
34+
}
35+
});
36+
37+
registry.registerBeanDefinition("aspectJExpressionPointcutAdvisor", beanDefinition.getBeanDefinition());
38+
39+
}
40+
}

quick-platform-component/src/main/java/com/quick/component/enables/QsEnableAroundLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.quick.component.enables;
22

3-
import com.quick.component.config.WebLogAspect;
3+
import com.quick.component.config.logAspect.WebLogAspectConfig;
44
import org.springframework.context.annotation.Import;
55

66
import java.lang.annotation.*;
@@ -11,7 +11,7 @@
1111
@Target(ElementType.TYPE)
1212
@Retention(RetentionPolicy.RUNTIME)
1313
@Documented
14-
@Import(WebLogAspect.class)
14+
@Import(WebLogAspectConfig.class)
1515
public @interface QsEnableAroundLog {
1616

1717
String value() default "execution(public * com.*..controller.*.*(..))";

0 commit comments

Comments
 (0)