Skip to content

Commit fcaf5f0

Browse files
committed
complete enable log 90%
1 parent 3adb723 commit fcaf5f0

File tree

8 files changed

+147
-69
lines changed

8 files changed

+147
-69
lines changed

quick-log/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
<version>1.0-SNAPSHOT</version>
3333
</dependency>
3434

35+
<dependency>
36+
<groupId>cn.hutool</groupId>
37+
<artifactId>hutool-all</artifactId>
38+
</dependency>
39+
3540
<dependency>
3641
<groupId>org.springframework.boot</groupId>
3742
<artifactId>spring-boot-starter-log4j2</artifactId>

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

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

3-
import com.quick.component.enables.EnableGlobalExceptionHandler;
3+
import com.quick.component.enables.QsEnableAroundLog;
4+
import com.quick.component.enables.QsEnableGlobalExceptionHandler;
45
import org.springframework.boot.SpringApplication;
56
import org.springframework.boot.autoconfigure.SpringBootApplication;
67
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -14,7 +15,8 @@
1415
*/
1516
@SpringBootApplication
1617
@EnableScheduling
17-
@EnableGlobalExceptionHandler
18+
@QsEnableGlobalExceptionHandler
19+
@QsEnableAroundLog
1820
public class Application {
1921
public static void main(String[] args) {
2022
SpringApplication.run(Application.class, args);

quick-log/src/main/java/com/quick/log/config/WebLogAspect.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

quick-log/src/main/java/com/quick/log/controller/ApiController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ public Map<String,Object> shotLog(@RequestParam("name")String name,@RequestParam
2727
Map<String,Object> result = new HashMap<>();
2828
result.put("name",name);
2929
result.put("age",age);
30-
for(int i=0;i<100000000;i++) {
31-
32-
loggerService.showLog();
33-
}
3430
return result;
3531
}
3632

quick-platform-component/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,23 @@
3333
<artifactId>spring-boot-starter-web</artifactId>
3434
</dependency>
3535

36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-aop</artifactId>
39+
</dependency>
40+
3641
<dependency>
3742
<groupId>org.springframework.boot</groupId>
3843
<artifactId>spring-boot-starter-test</artifactId>
3944
</dependency>
4045

46+
<dependency>
47+
<groupId>cn.hutool</groupId>
48+
<artifactId>hutool-all</artifactId>
49+
</dependency>
50+
51+
52+
4153

4254
<dependency>
4355
<groupId>org.projectlombok</groupId>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.quick.component.config;
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.aspectj.lang.ProceedingJoinPoint;
8+
import org.aspectj.lang.Signature;
9+
import org.aspectj.lang.annotation.Around;
10+
import org.aspectj.lang.annotation.Aspect;
11+
import org.aspectj.lang.reflect.MethodSignature;
12+
import org.springframework.util.StopWatch;
13+
import org.springframework.util.StringUtils;
14+
import org.springframework.web.bind.annotation.RequestBody;
15+
import org.springframework.web.bind.annotation.RequestParam;
16+
import org.springframework.web.context.request.RequestContextHolder;
17+
import org.springframework.web.context.request.ServletRequestAttributes;
18+
19+
import javax.servlet.http.HttpServletRequest;
20+
import java.lang.reflect.Method;
21+
import java.lang.reflect.Parameter;
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
@Aspect
28+
@Slf4j
29+
public class WebLogAspect {
30+
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()")
46+
@Around("execution(public * com..controller.*.*(..))")
47+
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
48+
//获取当前请求对象
49+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
50+
HttpServletRequest request = attributes.getRequest();
51+
//记录请求信息
52+
StopWatch stopWatch = new StopWatch();
53+
stopWatch.start();
54+
Object result = pjp.proceed();
55+
stopWatch.stop();
56+
57+
Signature signature = pjp.getSignature();
58+
MethodSignature methodSignature = (MethodSignature) signature;
59+
Method method = methodSignature.getMethod();
60+
String urlStr = request.getRequestURL().toString();
61+
log.info("===================================begin==================================================");
62+
log.info("req path: {}", StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
63+
log.info("req ip: {}", request.getRemoteUser());
64+
log.info("req method: {}", request.getMethod());
65+
log.info("req params: {}", getParameter(method, pjp.getArgs()));
66+
log.info("req result: {}", JSONUtil.toJsonStr(result));
67+
log.info("req uri: {}", request.getRequestURI());
68+
log.info("req url: {}", request.getRequestURL().toString());
69+
log.info("req cost: {}ms", stopWatch.getLastTaskTimeMillis());
70+
log.info("===================================end===================================================");
71+
return result;
72+
}
73+
74+
75+
/**
76+
* 根据方法和传入的参数获取请求参数
77+
*/
78+
private Object getParameter(Method method, Object[] args) {
79+
List<Object> argList = new ArrayList<>();
80+
Parameter[] parameters = method.getParameters();
81+
for (int i = 0; i < parameters.length; i++) {
82+
//将RequestBody注解修饰的参数作为请求参数
83+
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
84+
if (requestBody != null) {
85+
argList.add(args[i]);
86+
}
87+
//将RequestParam注解修饰的参数作为请求参数
88+
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
89+
if (requestParam != null) {
90+
Map<String, Object> map = new HashMap<>();
91+
String key = parameters[i].getName();
92+
if (!StringUtils.isEmpty(requestParam.value())) {
93+
key = requestParam.value();
94+
}
95+
map.put(key, args[i]);
96+
argList.add(map);
97+
}
98+
}
99+
if (argList.size() == 0) {
100+
return null;
101+
} else if (argList.size() == 1) {
102+
return argList.get(0);
103+
} else {
104+
return argList;
105+
}
106+
}
107+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.quick.component.enables;
2+
3+
import com.quick.component.config.WebLogAspect;
4+
import org.springframework.context.annotation.Import;
5+
6+
import java.lang.annotation.*;
7+
8+
/**
9+
* 接口请求日志
10+
*/
11+
@Target(ElementType.TYPE)
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Documented
14+
@Import(WebLogAspect.class)
15+
public @interface QsEnableAroundLog {
16+
17+
String value() default "execution(public * com.*..controller.*.*(..))";
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
@Retention(RetentionPolicy.RUNTIME)
1010
@Documented
1111
@Import(GlobalHandlerConfig.class)
12-
public @interface EnableGlobalExceptionHandler {
12+
public @interface QsEnableGlobalExceptionHandler {
1313
}

0 commit comments

Comments
 (0)