Skip to content

Commit

Permalink
Merge pull request #107 from mouzt/3.0.4
Browse files Browse the repository at this point in the history
3.0.4
  • Loading branch information
mouzt authored Jan 3, 2023
2 parents 727ba4f + 4ba31d0 commit defaecb
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 12 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.3-SNAPSHOT</version>
<version>3.0.4-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 @@ -7,8 +7,10 @@
import de.danielbechler.diff.comparison.ComparisonService;
import de.danielbechler.diff.node.DiffNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.AopUtils;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
Expand Down Expand Up @@ -37,18 +39,20 @@ public String diff(Object source, Object target) {
Class<?> clazz = source == null ? target.getClass() : source.getClass();
source = source == null ? clazz.getDeclaredConstructor().newInstance() : source;
target = target == null ? clazz.getDeclaredConstructor().newInstance() : target;
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
throw new RuntimeException(e);
}
}
if (!Objects.equals(source.getClass(), target.getClass())) {
if (!Objects.equals(AopUtils.getTargetClass(source.getClass()),AopUtils.getTargetClass(target.getClass()))) {
log.error("diff的两个对象类型不同, source.class={}, target.class={}", source.getClass().toString(), target.getClass().toString());
return "";
}
ObjectDifferBuilder objectDifferBuilder = ObjectDifferBuilder.startBuilding();
DiffNode diffNode = objectDifferBuilder
.differs().register((differDispatcher, nodeQueryService) ->
new ArrayDiffer(differDispatcher, (ComparisonService) objectDifferBuilder.comparison(), objectDifferBuilder.identity()))
.comparison().ofType(LocalDateTime.class).toUseEqualsMethod().and()
.build()
.compare(target, source);
return diffItemsToLogContentService.toLogContent(diffNode, source, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public class LogRecordProperties {
*/
private String ofWord = "的";

/**
* 是否不校验文案,全部记录日志
*/
private Boolean diffLog = false;

public void setAddTemplate(String template) {
validatePlaceHolder(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public DefaultParseFunction parseFunction() {

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public BeanFactoryLogRecordAdvisor logRecordAdvisor() {
public BeanFactoryLogRecordAdvisor logRecordAdvisor(LogRecordProperties logRecordProperties) {
BeanFactoryLogRecordAdvisor advisor =
new BeanFactoryLogRecordAdvisor();
advisor.setLogRecordOperationSource(logRecordOperationSource());
advisor.setAdvice(logRecordInterceptor());
advisor.setAdvice(logRecordInterceptor(logRecordProperties.getDiffLog()));
advisor.setOrder(enableLogRecord.getNumber("order"));
return advisor;
}
Expand All @@ -77,11 +77,12 @@ public ILogRecordPerformanceMonitor logRecordPerformanceMonitor() {

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public LogRecordInterceptor logRecordInterceptor() {
public LogRecordInterceptor logRecordInterceptor(Boolean diffLog) {
LogRecordInterceptor interceptor = new LogRecordInterceptor();
interceptor.setLogRecordOperationSource(logRecordOperationSource());
interceptor.setTenant(enableLogRecord.getString("tenant"));
interceptor.setJoinTransaction(enableLogRecord.getBoolean("joinTransaction"));
interceptor.setDiffLog(diffLog);
//interceptor.setLogFunctionParser(logFunctionParser(functionService));
//interceptor.setDiffParseFunction(diffParseFunction);
interceptor.setLogRecordPerformanceMonitor(logRecordPerformanceMonitor());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.mzt.logapi.starter.support;

import com.mzt.logapi.starter.annotation.EnableLogRecord;
import com.mzt.logapi.starter.configuration.LogRecordProxyAutoConfiguration;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.AdviceModeImportSelector;
import org.springframework.context.annotation.AutoProxyRegistrar;
import org.springframework.lang.Nullable;

import com.mzt.logapi.starter.annotation.EnableLogRecord;
import com.mzt.logapi.starter.configuration.LogRecordProxyAutoConfiguration;

/**
* DATE 6:57 PM
*
Expand All @@ -21,7 +22,7 @@ public String[] selectImports(AdviceMode adviceMode) {
case PROXY:
return new String[]{AutoProxyRegistrar.class.getName(), LogRecordProxyAutoConfiguration.class.getName()};
case ASPECTJ:
return new String[]{LogRecordProxyAutoConfiguration.class.toString()};
return new String[] {LogRecordProxyAutoConfiguration.class.getName()};
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class LogRecordInterceptor extends LogRecordValueParser implements Method

private boolean joinTransaction;

private boolean diffLog;

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
Expand Down Expand Up @@ -192,7 +194,8 @@ 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)) || Objects.equals(action, expressionValues.get(action))) {
if (StringUtils.isEmpty(expressionValues.get(action)) ||
(!diffLog && action.contains("#") && Objects.equals(action, expressionValues.get(action)))) {
return;
}
LogRecord logRecord = LogRecord.builder()
Expand Down Expand Up @@ -271,6 +274,10 @@ public void setJoinTransaction(boolean joinTransaction) {
this.joinTransaction = joinTransaction;
}

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

@Override
public void afterSingletonsInstantiated() {
bizLogService = beanFactory.getBean(ILogRecordService.class);
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.3-SNAPSHOT</version>
<version>3.0.4-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public interface IOrderService {
boolean testGlobalVariable(Order order);

boolean testGlobalVariableCover(Order order, User user);

void fixedCopy(String text);

void fixedCopy2(User user, User oldUser);
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,19 @@ public boolean testGlobalVariable(Order order) {
public boolean testGlobalVariableCover(Order order, User user) {
return false;
}

@Override
@LogRecord(success = "固定文案记录日志",
type = LogRecordType.USER, bizNo = "{{#text}}")
public void fixedCopy(String text) {
log.info("进入方法。。。");
}

@Override
@LogRecord(success = "更新了用户{_DIFF{#user, #oldUser}}",
type = LogRecordType.USER, bizNo = "{{#user.name}}",
extra = "{{#user.toString()}}")
public void fixedCopy2(User user, User oldUser) {

}
}
4 changes: 4 additions & 0 deletions bizlog-server/src/main/java/com/mzt/logserver/pojo/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mzt.logapi.starter.annotation.DIffLogIgnore;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

/**
Expand Down Expand Up @@ -56,6 +57,9 @@ public class User {

private String[] noLikeStrings;


private LocalDateTime localDateTime;

@Data
public static class Address {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.mzt.logserver;

import cn.hutool.extra.spring.SpringUtil;
import com.google.common.collect.Lists;
import com.mzt.logapi.beans.CodeVariableType;
import com.mzt.logapi.beans.LogRecord;
import com.mzt.logapi.starter.support.aop.LogRecordInterceptor;
import com.mzt.logserver.infrastructure.constants.LogRecordType;
import com.mzt.logserver.infrastructure.logrecord.service.DbLogRecordService;
import com.mzt.logserver.pojo.Order;
import com.mzt.logserver.pojo.User;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.context.jdbc.Sql;
Expand Down Expand Up @@ -628,4 +631,46 @@ public void testResultNoLog() {
Assert.assertEquals(0, logRecordList.size());
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void fixedCopy() {
String text = "text";
orderService.fixedCopy(text);
List<LogRecord> logRecordList = logRecordService.queryLog(text, LogRecordType.USER);
Assert.assertEquals(1, logRecordList.size());
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void fixedCopy2() {
// 记录日志
LogRecordInterceptor bean = SpringUtil.getBean(LogRecordInterceptor.class);
bean.setDiffLog(true);
User user = new User();
user.setName("张三");
User oldUser = new User();
oldUser.setName("张三");
orderService.fixedCopy2(user, oldUser);
List<LogRecord> logRecordList = logRecordService.queryLog(user.getName(), LogRecordType.USER);
Assert.assertEquals(1, logRecordList.size());
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void fixedCopy3() {
// 不记录日志
LogRecordInterceptor bean = SpringUtil.getBean(LogRecordInterceptor.class);
bean.setDiffLog(false);
User user = new User();
user.setName("张三");
User oldUser = new User();
oldUser.setName("张三");
orderService.fixedCopy2(user, oldUser);
List<LogRecord> logRecordList = logRecordService.queryLog(user.getName(), LogRecordType.USER);
Assert.assertEquals(0, logRecordList.size());
logRecordService.clean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.test.context.jdbc.Sql;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -364,4 +365,27 @@ public void testInterfaceAndAbstract2() {
Assert.assertEquals(logRecord.getOperator(), "111");
logRecordService.clean();
}

@Test
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void test_DIffLogIgnore_LocalDateTime() {
User user = new User();
user.setId(1L);
LocalDateTime now = LocalDateTime.now();
user.setLocalDateTime(now);

User newUser = new User();
newUser.setId(1L);
newUser.setLocalDateTime(LocalDateTime.MIN);

userService.diffUser(user, newUser);

List<LogRecord> logRecordList = logRecordService.queryLog(String.valueOf(user.getId()), LogRecordType.USER);
Assert.assertEquals(1, logRecordList.size());
LogRecord logRecord = logRecordList.get(0);
Assert.assertEquals(logRecord.getAction(), "更新了用户信息【localDateTime】从【" + now + "】修改为【-999999999-01-01T00:00】");
Assert.assertNotNull(logRecord.getExtra());
Assert.assertEquals(logRecord.getOperator(), "111");
logRecordService.clean();
}
}
15 changes: 14 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

| 版本 | 状态 |
|-------|-------------------------------------------------------------------------------------------------------------------------------------------------|
| 3.0.3 | 1.修复日志打印两次的问题 2.方法支持多注解(#98) 3.相同对象diff不记录日志 详细使用方式见 IOrderServiceTest |
| 3.0.4 | 1.修复fix:修复LocalDateTime diff (#111, #114), 2. 固定文案判断错误 |
| 3.0.3 | 1.修复日志打印两次的问题 2.方法支持多注解(#98) 3.相同对象diff不记录日志 详细使用方式见 IOrderServiceTest |
| 3.0.2 | 1.修复 DIffLogIgnore注解在集合类型上失效问题 2.支持跨方法的全局变量 3. 支持日志记录异常与业务逻辑一起回滚的逻辑,默认日志记录不影响业务逻辑 |
| 3.0.1 | diff 功能支持了数组(https://github.com/mouzt/mzt-biz-log/issues/75) ,增加判断是否成功的条件表达式,增加 @DiffLogAllFields@DIffLogIgnore 注解支持 |
| 3.0.0 | 暂时删除了list实现优化中,增加了xml的方式,增加了性能监控接口,修复了function 内的 service 需要添加 @Lazy 的问题 || 2.0.2 | 1.修复了 LogFunctionParser 的NPE,2. 注解上添加了ElementType.TYPE,3.记录了当前执行方法的Class和Method 4. 重新fix了没有加EnableTransactionManagement 切面不生效的逻辑 5. 增加了 Subtype 的 SpEl解析 |
Expand Down Expand Up @@ -734,6 +735,18 @@ public class UserParseFunction implements IParseFunction {

⚠️ 整体日志拦截是在方法执行之后记录的,所以对于方法内部修改了方法参数之后,LogRecord 的注解上的 SpEL 对变量的取值是修改后的值哦~

#### 常见问题:
- 为什么有的类注解生效了,有的类注解未生效?
> 此问题和bean的生命周期相关,确定未生效的类是否被提前初始化,即在`BeanFactoryLogRecordAdvisor`之前已经被加载
- 为何没记录日志?
> 1. 默认比对对象无变动时不记录日志,可通过配置文件`mzt.log.record.diffLog`修改,默认为false,无变动时不记录日志
> 2.`mzt.log.record.diffLog=false`时,且文案中包含#,对象比对后未发生改变,会跳过日志
- 如何提问?
> 提问前请确定已经阅读上面使用文档,一个好的问题请参考:[提問的智慧](https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way)

## Author

mail : [email protected]
Expand Down

0 comments on commit defaecb

Please sign in to comment.