Skip to content

Commit 8f39db1

Browse files
authored
Merge pull request #191 from KEHFAN/20240708-recordOperation
20240708 record operation
2 parents 9da33ff + dcdfef3 commit 8f39db1

5 files changed

Lines changed: 112 additions & 4 deletions

File tree

record-operation/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ record-operation一个利用Java提供的AOP机制来打印请求日志的依赖
5252

5353
注意:如果为其它字符则会导致类型不一致导致发布失败。需要去排查你的应用参数是否输入正确。
5454

55+
2.方法:saveLog(String log)
56+
1. - 功能:用于存储日志数据
57+
- 参数:log,拦截的日志信息
58+
- 返回值:Boolean
59+
- 用途:用户自定义日志存储逻辑
60+
61+
用户可直接双击saveLog,自定义log的解析逻辑,并将其按指定的方式存储(文件、数据库)
62+
![img.png](覆写saveLog逻辑.png)
63+
5564
## 应用演示链接
5665

5766
1.引入依赖库

record-operation/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.wgx</groupId>
88
<artifactId>record-operation</artifactId>
9-
<version>1.0.4</version>
9+
<version>1.0.7</version>
1010
<name>操作记录依赖库</name>
1111
<description>打印每次请求日志:全类名+方法名、请求ip,请求url,入参、出参;出现异常时打印error日志:异常信息、异常方法、异常栈、请求参数、请求IP</description>
1212

@@ -54,7 +54,6 @@
5454
<scope>provided</scope>
5555
<version>1.7.30</version>
5656
</dependency>
57-
5857
<dependency>
5958
<artifactId>nasl-metadata-collector</artifactId>
6059
<groupId>com.netease.lowcode</groupId>

record-operation/src/main/java/com/wgx/aop/LoggingAspect.java

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44
import com.netease.lowcode.core.EnvironmentType;
55
import com.netease.lowcode.core.annotation.Environment;
66
import com.netease.lowcode.core.annotation.NaslConfiguration;
7+
import com.wgx.logics.DataWriter;
78
import org.aspectj.lang.ProceedingJoinPoint;
89
import org.aspectj.lang.annotation.Around;
910
import org.aspectj.lang.annotation.Aspect;
1011
import org.aspectj.lang.annotation.Pointcut;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
14+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
15+
import org.springframework.beans.factory.annotation.Autowired;
1316
import org.springframework.beans.factory.annotation.Value;
17+
import org.springframework.context.ApplicationContext;
1418
import org.springframework.core.annotation.Order;
1519
import org.springframework.stereotype.Component;
1620
import org.springframework.web.context.request.RequestContextHolder;
1721
import org.springframework.web.context.request.ServletRequestAttributes;
22+
import org.springframework.web.multipart.MultipartFile;
1823

1924
import javax.servlet.http.HttpServletRequest;
25+
import javax.servlet.http.HttpServletResponse;
26+
import java.lang.reflect.Method;
27+
import java.text.MessageFormat;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
import java.util.Objects;
32+
import java.util.stream.Collectors;
2033

2134

2235
@Aspect
@@ -52,6 +65,9 @@ public class LoggingAspect {
5265
})
5366
private String loggingClassNames;
5467

68+
@Autowired
69+
private ApplicationContext applicationContext;
70+
5571
//切点匹配类上有@Controller、@RestController注解的类下的所有方法
5672
@Pointcut("within(@org.springframework.stereotype.Controller *) || within(@org.springframework.web.bind.annotation.RestController *)")
5773
public void controller() {}
@@ -62,6 +78,15 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
6278
return joinPoint.proceed();
6379
}
6480

81+
Object recordService = null;
82+
Method saveLog = null;
83+
try {
84+
recordService = applicationContext.getBean("saveLogOverriddenRecord_operationCustomizeService");
85+
saveLog = recordService.getClass().getMethod("saveLogOverriddenRecord_operation", String.class);
86+
} catch (NoSuchBeanDefinitionException e) {
87+
// do nothing
88+
}
89+
6590
long startTime = System.currentTimeMillis();
6691

6792
// Ensure the current request attributes are available.
@@ -80,35 +105,59 @@ public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
80105
} catch (Throwable ex) {
81106
exceptionThrown = true;
82107
if ("error".equals(loggingFormat)) {
108+
String msg = MessageFormat.format("Error in {0}#{1} - IP: {2} - URL: {3} - Args: {4} - Exception: {5}",
109+
joinPoint.getSignature().getDeclaringTypeName(),
110+
joinPoint.getSignature().getName(),
111+
requestIP,
112+
requestURL,
113+
serializeArgs(args),
114+
ex.toString());
83115
logger.error("Error in {}#{} - IP: {} - URL: {} - Args: {} - Exception: {}",
84116
joinPoint.getSignature().getDeclaringTypeName(),
85117
joinPoint.getSignature().getName(),
86118
requestIP,
87119
requestURL,
88-
serializeObject(args),
120+
serializeArgs(args),
89121
ex.toString());
90122
logger.error("Exception stack trace:", ex);
123+
DataWriter.invoke(recordService, saveLog, msg);
91124
}
92125
throw ex; // rethrow the exception
93126
} finally {
94127
long executionTime = System.currentTimeMillis() - startTime;
95128
if (!exceptionThrown) {
96129
if ("simple".equals(loggingFormat)) {
130+
String msg = MessageFormat.format("Completed {0}#{} - IP: {1} - URL: {2} - Duration: {3} ms",
131+
joinPoint.getSignature().getDeclaringTypeName(),
132+
joinPoint.getSignature().getName(),
133+
requestIP,
134+
requestURL,
135+
executionTime);
97136
logger.info("Completed {}#{} - IP: {} - URL: {} - Duration: {} ms",
98137
joinPoint.getSignature().getDeclaringTypeName(),
99138
joinPoint.getSignature().getName(),
100139
requestIP,
101140
requestURL,
102141
executionTime);
142+
DataWriter.invoke(recordService, saveLog, msg);
103143
}else if ("detailed".equals(loggingFormat)) {
144+
String msg = MessageFormat.format("Completed {0}#{1} - IP: {2} - URL: {3} - Args: {4} - Result: {5} - Duration: {6} ms",
145+
joinPoint.getSignature().getDeclaringTypeName(),
146+
joinPoint.getSignature().getName(),
147+
requestIP,
148+
requestURL,
149+
serializeArgs(args),
150+
serializeObject(result),
151+
executionTime);
104152
logger.info("Completed {}#{} - IP: {} - URL: {} - Args: {} - Result: {} - Duration: {} ms",
105153
joinPoint.getSignature().getDeclaringTypeName(),
106154
joinPoint.getSignature().getName(),
107155
requestIP,
108156
requestURL,
109-
serializeObject(args),
157+
serializeArgs(args),
110158
serializeObject(result),
111159
executionTime);
160+
DataWriter.invoke(recordService, saveLog, msg);
112161
}
113162
}
114163
}
@@ -141,6 +190,27 @@ private boolean shouldLog(ProceedingJoinPoint joinPoint) {
141190
return true;
142191
}
143192

193+
private String serializeArgs(Object[] args) {
194+
if (Objects.nonNull(args)) {
195+
List<Object> objList = new ArrayList<>();
196+
Arrays.asList(args).forEach(obj -> {
197+
if (obj instanceof HttpServletRequest || obj instanceof HttpServletResponse) {
198+
return;
199+
}
200+
if (obj instanceof List) {
201+
List<?> argsObjList = (List<?>) obj;
202+
if (Objects.nonNull(argsObjList) && !argsObjList.isEmpty() && argsObjList.get(0) instanceof MultipartFile) {
203+
return;
204+
}
205+
}
206+
objList.add(obj);
207+
});
208+
209+
return JSON.toJSONString(objList.toArray());
210+
}
211+
return JSON.toJSONString(args);
212+
}
213+
144214
private String serializeObject(Object obj) {
145215
try {
146216
return JSON.toJSONString(obj);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.wgx.logics;
2+
3+
import com.netease.lowcode.core.annotation.NaslLogic;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
8+
public class DataWriter {
9+
10+
/**
11+
* 用户覆写,不要修改方法名和参数列表
12+
*
13+
* @param log
14+
* @return
15+
*/
16+
@NaslLogic(override = true,enhance = false)
17+
public static Boolean saveLog(String log) {
18+
19+
return true;
20+
}
21+
22+
public static void invoke(Object service,Method saveLog,String log) throws InvocationTargetException, IllegalAccessException {
23+
if(saveLog == null){
24+
saveLog(log);
25+
return;
26+
}
27+
saveLog.invoke(service,log);
28+
}
29+
30+
}
95.9 KB
Loading

0 commit comments

Comments
 (0)