Skip to content

Commit

Permalink
feat: manual log
Browse files Browse the repository at this point in the history
version: dependency update
refactor: test class
docs: README
  • Loading branch information
qqxx6661 committed Jun 1, 2024
1 parent 058341e commit 6e26806
Show file tree
Hide file tree
Showing 29 changed files with 209 additions and 57 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,31 @@ public class CustomThreadPoolProvider implements ThreadPoolProvider {

`@OperationLog`注解提供布尔值`recordReturnValue()`用于是否开启记录函数返回值,默认关闭,防止返回值实体过大,造成序列化时性能消耗过多。

### 非注解方式

在实际业务场景中,很多时候由于注解的限制,无法很好的使用注解记录日志,此时可以使用纯手动的方式进行日志记录。

框架提供了手动记录日志的方法:

cn.monitor4all.logRecord.util.OperationLogUtil

```java
LogRequest logRequest = LogRequest.builder()
.bizId("testBizId")
.bizType("testBuildLogRequest")
.success(true)
.msg("testMsg")
.tag("testTag")
.returnStr("testReturnStr")
.extra("testExtra")
// 其他字段
.build();
OperationLogUtil.log(logRequest);
```

使用该方式记录日志,注解带来的相关功能则无法使用,如SpEL表达式,自定义函数等。


### 操作日志数据表结构推荐

以MySQL表为例:
Expand Down
40 changes: 20 additions & 20 deletions log-record-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,24 +166,24 @@
<!-- JDK17 -->
<!-- <doc.path>${java.home}/bin/javadoc</doc.path>-->
</properties>
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-gpg-plugin</artifactId>-->
<!-- <version>1.5</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>sign-artifacts</id>-->
<!-- <phase>verify</phase>-->
<!-- <goals>-->
<!-- <goal>sign</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Expand Down Expand Up @@ -216,7 +216,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -227,7 +227,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cn.monitor4all.logRecord.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringContextUtil implements ApplicationContextAware {

private static ApplicationContext applicationContext;
Expand All @@ -15,6 +18,11 @@ public void setApplicationContext(ApplicationContext applicationContext) {
}

public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
try {
return applicationContext.getBean(clazz);
} catch (BeansException e) {
log.info("Bean {} not existed, default return null.", clazz.getName());
return null;
}
}
}
6 changes: 3 additions & 3 deletions log-record-springboot3-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>cn.monitor4all</groupId>
<artifactId>log-record-springboot3-starter</artifactId>
<version>1.6.3</version>
<version>1.7.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -35,7 +35,7 @@
<dependency>
<groupId>cn.monitor4all</groupId>
<artifactId>log-record-core</artifactId>
<version>1.6.3</version>
<version>1.7.0-SNAPSHOT</version>
</dependency>

<!-- 单元测试依赖 -->
Expand Down Expand Up @@ -171,7 +171,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
LogRecordAutoConfiguration.class,
OperatorIdGetService.class,
TestService.class,})
@PropertySource("classpath:testNormal.properties")
@PropertySource("classpath:testCommon.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OperationLogNormalTest {
public class OperationLogCommonTest {

@Autowired
private TestService testService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cn.monitor4all.logRecord.springboot3.test;


import cn.monitor4all.logRecord.bean.LogDTO;
import cn.monitor4all.logRecord.bean.LogRequest;
import cn.monitor4all.logRecord.springboot3.LogRecordAutoConfiguration;
import cn.monitor4all.logRecord.springboot3.test.service.OperatorIdGetService;
import cn.monitor4all.logRecord.springboot3.test.service.TestService;
import cn.monitor4all.logRecord.springboot3.test.utils.TestHelper;
import cn.monitor4all.logRecord.util.OperationLogUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.test.context.ContextConfiguration;

import java.util.Date;

/**
* 单元测试:自定义线程池
*/
@Slf4j
@SpringBootTest
@ContextConfiguration(classes = {
LogRecordAutoConfiguration.class,
OperatorIdGetService.class,
TestService.class,})
@PropertySource("classpath:testCustomLog.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OperationLogCustomLogTest {

@Test
public void testBuildLogRequest() {
TestHelper.addLock("testBuildLogRequest");
Date date = new Date();
LogRequest logRequest = LogRequest.builder()
.bizId("testBizId")
.bizType("testBuildLogRequest")
.exception("testException")
.operateDate(date)
.success(true)
.msg("testMsg")
.tag("testTag")
.returnStr("testReturnStr")
.executionTime(0L)
.extra("testExtra")
.operatorId("testOperatorId")
.build();
OperationLogUtil.log(logRequest);
TestHelper.await("testBuildLogRequest");
LogDTO logDTO = TestHelper.getLogDTO("testBuildLogRequest");

Assertions.assertEquals("testBizId", logDTO.getBizId());
Assertions.assertEquals("testBuildLogRequest", logDTO.getBizType());
Assertions.assertEquals("testException", logDTO.getException());
Assertions.assertEquals(date, logDTO.getOperateDate());
Assertions.assertEquals(true, logDTO.getSuccess());
Assertions.assertEquals("testMsg", logDTO.getMsg());
Assertions.assertEquals("testTag", logDTO.getTag());
Assertions.assertEquals("testReturnStr", logDTO.getReturnStr());
Assertions.assertEquals(0L, logDTO.getExecutionTime());
Assertions.assertEquals("testExtra", logDTO.getExtra());
Assertions.assertEquals("testOperatorId", logDTO.getOperatorId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
LogRecordAutoConfiguration.class,
OperatorIdGetService.class,
TestService.class,})
@PropertySource("classpath:testLogRecordDiffIgnoreNullValue.properties")
@PropertySource("classpath:testDiffIgnoreNullValue.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class LogRecordDiffIgnoreNullValueTest {
public class OperationLogDiffIgnoreNullValueTest {

@Autowired
private TestService testService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

@Slf4j
@TestComponent
@ConditionalOnProperty(name = "test.config", havingValue = "normal")
public class OperationLogNormalService implements IOperationLogGetService {
@ConditionalOnProperty(name = "test.config", havingValue = "common")
public class OperationLogCommonGetService implements IOperationLogGetService {

@Override
public boolean createLog(LogDTO logDTO) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.monitor4all.logRecord.springboot3.test.service;

import cn.monitor4all.logRecord.bean.LogDTO;
import cn.monitor4all.logRecord.service.IOperationLogGetService;
import cn.monitor4all.logRecord.springboot3.test.utils.TestHelper;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.test.context.TestComponent;

@Slf4j
@TestComponent
@ConditionalOnProperty(name = "test.config", havingValue = "customLog")
public class OperationLogCustomLogGetService implements IOperationLogGetService {

@Override
public boolean createLog(LogDTO logDTO) throws Exception {
log.info("logDTO: [{}]", JSON.toJSONString(logDTO));

if ("testBuildLogRequest".equals(logDTO.getBizType())) {
TestHelper.putLogDTO("testBuildLogRequest", logDTO);
TestHelper.releaseLock("testBuildLogRequest");
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Slf4j
@TestComponent
@ConditionalOnProperty(name = "test.config", havingValue = "customThreadPool")
public class OperationLogGetCustomThreadPoolService implements IOperationLogGetService {
public class OperationLogCustomThreadPoolGetService implements IOperationLogGetService {

@Override
public boolean createLog(LogDTO logDTO) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

@Slf4j
@TestComponent
@ConditionalOnProperty(name = "test.config", havingValue = "logRecordDiffIgnoreNullValue")
public class TestLogRecordDiffIgnoreNullValueService implements IOperationLogGetService {
@ConditionalOnProperty(name = "test.config", havingValue = "diffIgnoreNullValue")
public class OperationLogDiffIgnoreNullValueGetService implements IOperationLogGetService {

@Override
public boolean createLog(LogDTO logDTO) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Slf4j
@TestComponent
@ConditionalOnProperty(name = "test.config", havingValue = "exception")
public class OperationLogExceptionService implements IOperationLogGetService {
public class OperationLogExceptionGetService implements IOperationLogGetService {

@Override
public boolean createLog(LogDTO logDTO) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.config=common
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test.config=customLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test.config=logRecordDiffIgnoreNullValue
test.config=diffIgnoreNullValue
log-record.diff-ignore-new-object-null-value=true
log-record.diff-ignore-old-object-null-value=true

This file was deleted.

4 changes: 2 additions & 2 deletions log-record-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand All @@ -170,7 +170,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
LogRecordAutoConfiguration.class,
OperatorIdGetService.class,
TestService.class,})
@PropertySource("classpath:testNormal.properties")
@PropertySource("classpath:testCommon.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OperationLogNormalTest {
public class OperationLogCommonTest {

@Autowired
private TestService testService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
LogRecordAutoConfiguration.class,
OperatorIdGetService.class,
TestService.class,})
@PropertySource("classpath:testNormal.properties")
@PropertySource("classpath:testCustomLog.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class OperationLogUtilTest {
public class OperationLogCustomLogTest {

@Test
public void testBuildLogRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
LogRecordAutoConfiguration.class,
OperatorIdGetService.class,
TestService.class,})
@PropertySource("classpath:testLogRecordDiffIgnoreNullValue.properties")
@PropertySource("classpath:testDiffIgnoreNullValue.properties")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class LogRecordDiffIgnoreNullValueTest {
public class OperationLogDiffIgnoreNullValueTest {

@Autowired
private TestService testService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

@Slf4j
@TestComponent
@ConditionalOnProperty(name = "test.config", havingValue = "normal")
public class OperationLogNormalService implements IOperationLogGetService {
@ConditionalOnProperty(name = "test.config", havingValue = "common")
public class OperationLogCommonGetService implements IOperationLogGetService {

@Override
public boolean createLog(LogDTO logDTO) {
Expand Down Expand Up @@ -225,11 +225,6 @@ public boolean createLog(LogDTO logDTO) {
TestHelper.releaseLock("testLogRecordContextTransmittableThreadLocal");
}

if ("testBuildLogRequest".equals(logDTO.getBizType())) {
TestHelper.putLogDTO("testBuildLogRequest", logDTO);
TestHelper.releaseLock("testBuildLogRequest");
}

return true;
}
}
Loading

0 comments on commit 6e26806

Please sign in to comment.