Skip to content

Commit

Permalink
* #153
Browse files Browse the repository at this point in the history
* 单测更新
* context 特殊情况下NPE
  • Loading branch information
muzhantong committed Jun 22, 2024
1 parent 1782d88 commit 4255802
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bizlog-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>bizlog-sdk</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.0.7-SNAPSHOT</version>
<parent>
<groupId>io.github.mouzt</groupId>
<artifactId>mzt-biz-log</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public LogRecordInterceptor logRecordInterceptor(LogRecordProperties logRecordPr
interceptor.setLogRecordOperationSource(logRecordOperationSource());
interceptor.setTenant(enableLogRecord.getString("tenant"));
interceptor.setJoinTransaction(enableLogRecord.getBoolean("joinTransaction"));
interceptor.setDiffLog(logRecordProperties.getDiffLog());
interceptor.setDiffSameWhetherSaveLog(logRecordProperties.getDiffLog());
//interceptor.setLogFunctionParser(logFunctionParser(functionService));
//interceptor.setDiffParseFunction(diffParseFunction);
interceptor.setLogRecordPerformanceMonitor(logRecordPerformanceMonitor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private boolean exitsCondition(MethodExecuteResult methodExecuteResult,
private void saveLog(Method method, boolean flag, LogRecordOps operation, String operatorIdFromService,
String action, Map<String, String> expressionValues) {
if (StringUtils.isEmpty(expressionValues.get(action)) ||
(!diffLog && action.contains("#") && Objects.equals(action, expressionValues.get(action)))) {
(!diffSameWhetherSaveLog && action.contains("#") && Objects.equals(action, expressionValues.get(action)))) {
return;
}
LogRecord logRecord = LogRecord.builder()
Expand Down Expand Up @@ -272,8 +272,8 @@ public void setJoinTransaction(boolean joinTransaction) {
this.joinTransaction = joinTransaction;
}

public void setDiffLog(boolean diffLog) {
this.diffLog = diffLog;
public void setDiffSameWhetherSaveLog(boolean diffLog) {
this.diffSameWhetherSaveLog = diffLog;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class LogRecordValueParser implements BeanFactoryAware {
public static final String COMMA = ",";
private final LogRecordExpressionEvaluator expressionEvaluator = new LogRecordExpressionEvaluator();
protected BeanFactory beanFactory;
protected boolean diffLog;
protected boolean diffSameWhetherSaveLog;

private LogFunctionParser logFunctionParser;

Expand Down Expand Up @@ -61,24 +61,21 @@ public Map<String, String> processTemplate(Collection<String> templates, MethodE
Matcher matcher = pattern.matcher(expressionTemplate);
StringBuffer parsedStr = new StringBuffer();
AnnotatedElementKey annotatedElementKey = new AnnotatedElementKey(methodExecuteResult.getMethod(), methodExecuteResult.getTargetClass());
boolean diffLogFlag = !diffLog;
boolean sameDiff = false;
while (matcher.find()) {

String expression = matcher.group(2);
String functionName = matcher.group(1);
if (DiffParseFunction.diffFunctionName.equals(functionName)) {
expression = getDiffFunctionValue(evaluationContext, annotatedElementKey, expression);
sameDiff = Objects.equals("", expression);
} else {
Object value = expressionEvaluator.parseExpression(expression, annotatedElementKey, evaluationContext);
expression = logFunctionParser.getFunctionReturnValue(beforeFunctionNameAndReturnMap, value, expression, functionName);
}
if (expression != null && !Objects.equals(expression, "")) {
diffLogFlag = false;
}
matcher.appendReplacement(parsedStr, Matcher.quoteReplacement(expression == null ? "" : expression));
}
matcher.appendTail(parsedStr);
expressionValues.put(expressionTemplate, diffLogFlag ? expressionTemplate : parsedStr.toString());
expressionValues.put(expressionTemplate, recordSameDiff(sameDiff, diffSameWhetherSaveLog) ? parsedStr.toString() : expressionTemplate);
} else {
expressionValues.put(expressionTemplate, expressionTemplate);
}
Expand All @@ -87,6 +84,16 @@ public Map<String, String> processTemplate(Collection<String> templates, MethodE
return expressionValues;
}

private boolean recordSameDiff(boolean sameDiff, boolean diffSameWhetherSaveLog) {
if(diffSameWhetherSaveLog == true) {
return true;
}
if(!diffSameWhetherSaveLog && sameDiff) {
return false;
}
return true;
}

private String getDiffFunctionValue(EvaluationContext evaluationContext, AnnotatedElementKey annotatedElementKey, String expression) {
String[] params = parseDiffFunction(expression);
if (params.length == 1) {
Expand Down
2 changes: 1 addition & 1 deletion bizlog-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>io.github.mouzt</groupId>
<artifactId>bizlog-sdk</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.0.7-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
**/
public interface IUserService {
boolean diffUser(User user, User newUser);
boolean diffUserByTwoExpression(User user, User newUser);

boolean testGlobalVariable(User user, Order order);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public boolean diffUser(User user, User newUser) {
return false;
}

@LogRecord(success = "{{#user.id}}更新了用户信息{_DIFF{#user, #newUser}}",
type = LogRecordType.USER, bizNo = "{{#newUser.id}}",
extra = "{{#newUser.toString()}}")
public boolean diffUserByTwoExpression(User user, User newUser) {
return false;
}

@Override
@LogRecord(success = "更新{{#user.name}}用户积分信息",
type = LogRecordType.USER, bizNo = "{{#user.id}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public void fixedCopy() {
public void fixedCopy2() {
// 记录日志
LogRecordInterceptor bean = SpringUtil.getBean(LogRecordInterceptor.class);
bean.setDiffLog(true);
bean.setDiffSameWhetherSaveLog(true);
User user = new User();
user.setName("张三");
User oldUser = new User();
Expand All @@ -664,7 +664,7 @@ public void fixedCopy2() {
public void fixedCopy3() {
// 不记录日志
LogRecordInterceptor bean = SpringUtil.getBean(LogRecordInterceptor.class);
bean.setDiffLog(false);
bean.setDiffSameWhetherSaveLog(false);
User user = new User();
user.setName("张三");
User oldUser = new User();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,34 @@ public void test_LocalDateTime() {
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void test_sameDiffNotRecord() {
User user = new User();
user.setId(1L);
User newUser = new User();
newUser.setId(1L);
userService.diffUser(user, newUser);

List<LogRecord> logRecordList = logRecordService.queryLog(String.valueOf(user.getId()), LogRecordType.USER);
Assert.assertEquals(0, logRecordList.size());
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void test_sameDiffNotRecordWithMoreExpression() {
User user = new User();
user.setId(1L);
User newUser = new User();
newUser.setId(1L);
userService.diffUserByTwoExpression(user, newUser);

List<LogRecord> logRecordList = logRecordService.queryLog(String.valueOf(user.getId()), LogRecordType.USER);
Assert.assertEquals(0, logRecordList.size());
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void test_LocalDate() {
Expand Down Expand Up @@ -422,7 +450,7 @@ public void test_LocalDate() {

public void test_diffLog_true() {
LogRecordInterceptor bean = SpringUtil.getBean(LogRecordInterceptor.class);
bean.setDiffLog(true);
bean.setDiffSameWhetherSaveLog(true);
User user = new User();
user.setId(1L);
user.setName("张三");
Expand Down Expand Up @@ -465,7 +493,7 @@ public void test_testGlobalVariable_diff() {
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void test_diffLog_false() {
LogRecordInterceptor bean = SpringUtil.getBean(LogRecordInterceptor.class);
bean.setDiffLog(false);
bean.setDiffSameWhetherSaveLog(false);
User user = new User();
user.setId(1L);
user.setName("张三");
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>io.github.mouzt</groupId>
<artifactId>bizlog-sdk</artifactId>
<version>3.0.5</version>
<version>3.0.7-SNAPSHOT</version>
</dependency>
```
#### SpringBoot入口打开开关,添加 @EnableLogRecord 注解
Expand Down

0 comments on commit 4255802

Please sign in to comment.