Skip to content

Commit

Permalink
Merge pull request #107 from qqxx6661/dev_1.7.x
Browse files Browse the repository at this point in the history
manual log support  / 支持静态类(非注解方式)记录日志
  • Loading branch information
qqxx6661 authored Jun 1, 2024
2 parents a38bae4 + 90724cb commit 57ba684
Show file tree
Hide file tree
Showing 33 changed files with 485 additions and 94 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
6 changes: 3 additions & 3 deletions log-record-core/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-core</artifactId>
<version>1.6.3</version>
<version>1.7.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
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
Expand Up @@ -2,13 +2,10 @@

import cn.monitor4all.logRecord.annotation.OperationLog;
import cn.monitor4all.logRecord.bean.LogDTO;
import cn.monitor4all.logRecord.configuration.LogRecordProperties;
import cn.monitor4all.logRecord.context.LogRecordContext;
import cn.monitor4all.logRecord.function.CustomFunctionRegistrar;
import cn.monitor4all.logRecord.service.DataPipelineService;
import cn.monitor4all.logRecord.service.IOperationLogGetService;
import cn.monitor4all.logRecord.handler.OperationLogHandler;
import cn.monitor4all.logRecord.service.IOperatorIdGetService;
import cn.monitor4all.logRecord.service.LogRecordErrorHandlerService;
import cn.monitor4all.logRecord.thread.LogRecordThreadPool;
import cn.monitor4all.logRecord.util.JsonUtil;
import com.alibaba.ttl.TtlRunnable;
Expand Down Expand Up @@ -43,23 +40,14 @@
public class SystemLogAspect {

@Autowired
private LogRecordProperties logRecordProperties;
private OperationLogHandler operationLogHandler;

@Autowired(required = false)
private LogRecordThreadPool logRecordThreadPool;

@Autowired(required = false)
private DataPipelineService dataPipelineService;

@Autowired(required = false)
private IOperationLogGetService iOperationLogGetService;

@Autowired(required = false)
private IOperatorIdGetService iOperatorIdGetService;

@Autowired(required = false)
private LogRecordErrorHandlerService logRecordErrorHandlerService;

private final SpelExpressionParser parser = new SpelExpressionParser();

private final DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
Expand Down Expand Up @@ -168,7 +156,7 @@ public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
try {
// 提交日志至主线程或线程池
Long finalExecutionTime = executionTime;
Consumer<LogDTO> createLogFunction = logDTO -> createLog(logDTO, finalExecutionTime);
Consumer<LogDTO> createLogFunction = logDTO -> operationLogHandler.createLog(logDTO, finalExecutionTime);
if (logRecordThreadPool != null) {
logDTOList.forEach(logDTO -> {
Runnable task = () -> createLogFunction.accept(logDTO);
Expand Down Expand Up @@ -321,49 +309,4 @@ private Method getMethod(JoinPoint joinPoint) {
}
return method;
}

private void createLog(LogDTO logDTO, Long finalExecutionTime) {
int maxRetryTimes = logRecordProperties.getRetry().getRetryTimes();

// 发送日志本地监听
boolean iOperationLogGetResult = false;
if (iOperationLogGetService != null) {
for (int retryTimes = 0; retryTimes <= maxRetryTimes; retryTimes++) {
try {
logDTO.setExecutionTime(finalExecutionTime);
iOperationLogGetResult = iOperationLogGetService.createLog(logDTO);
if (iOperationLogGetResult) {
break;
}
} catch (Throwable throwable) {
log.error("OperationLogAspect send logDTO error", throwable);
}
}
}

if (!iOperationLogGetResult && iOperationLogGetService != null && logRecordErrorHandlerService != null) {
logRecordErrorHandlerService.operationLogGetErrorHandler();
}

// 发送消息管道
boolean dataPipelineServiceResult = false;
if (dataPipelineService != null) {
for (int retryTimes = 0; retryTimes <= maxRetryTimes; retryTimes++) {
try {
logDTO.setExecutionTime(finalExecutionTime);
dataPipelineServiceResult = dataPipelineService.createLog(logDTO);
if (dataPipelineServiceResult) {
break;
}
} catch (Throwable throwable) {
log.error("OperationLogAspect send logDTO error", throwable);
}
}
}

if (!dataPipelineServiceResult && dataPipelineService != null && logRecordErrorHandlerService != null) {
logRecordErrorHandlerService.dataPipelineErrorHandler();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cn.monitor4all.logRecord.bean;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
* 日志请求实体
* 用于非注解手动记录日志
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class LogRequest {

/**
* 业务ID
*/
private String bizId;
/**
* 业务类型
*/
private String bizType;
/**
* 方法异常信息
*/
private String exception;
/**
* 日志操作时间
*/
private Date operateDate;
/**
* 方法是否成功
*/
private Boolean success;
/**
* 日志内容
*/
private String msg;
/**
* 日志标签
*/
private String tag;
/**
* 方法结果
*/
private String returnStr;
/**
* 方法执行时间(单位:毫秒)
*/
private Long executionTime;
/**
* 额外信息
*/
private String extra;
/**
* 操作人ID
*/
private String operatorId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cn.monitor4all.logRecord.handler;


import cn.monitor4all.logRecord.bean.LogDTO;
import cn.monitor4all.logRecord.configuration.LogRecordProperties;
import cn.monitor4all.logRecord.service.DataPipelineService;
import cn.monitor4all.logRecord.service.IOperationLogGetService;
import cn.monitor4all.logRecord.service.LogRecordErrorHandlerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class OperationLogHandler {

@Autowired
private LogRecordProperties logRecordProperties;

@Autowired(required = false)
private DataPipelineService dataPipelineService;

@Autowired(required = false)
private IOperationLogGetService iOperationLogGetService;

@Autowired(required = false)
private LogRecordErrorHandlerService logRecordErrorHandlerService;

public void createLog(LogDTO logDTO, Long finalExecutionTime) {

// 重试次数
int maxRetryTimes = logRecordProperties.getRetry().getRetryTimes();

// 发送本地日志
boolean iOperationLogGetResult = false;
if (iOperationLogGetService != null) {
for (int retryTimes = 0; retryTimes <= maxRetryTimes; retryTimes++) {
try {
logDTO.setExecutionTime(finalExecutionTime);
iOperationLogGetResult = iOperationLogGetService.createLog(logDTO);
if (iOperationLogGetResult) {
break;
}
} catch (Throwable throwable) {
log.error("OperationLogAspect send logDTO error", throwable);
}
}
}

// 发送本地日志失败错误处理
if (!iOperationLogGetResult && iOperationLogGetService != null && logRecordErrorHandlerService != null) {
logRecordErrorHandlerService.operationLogGetErrorHandler();
}

// 发送消息管道
boolean dataPipelineServiceResult = false;
if (dataPipelineService != null) {
for (int retryTimes = 0; retryTimes <= maxRetryTimes; retryTimes++) {
try {
logDTO.setExecutionTime(finalExecutionTime);
dataPipelineServiceResult = dataPipelineService.createLog(logDTO);
if (dataPipelineServiceResult) {
break;
}
} catch (Throwable throwable) {
log.error("OperationLogAspect send logDTO error", throwable);
}
}
}

// 发送消息管道失败错误处理
if (!dataPipelineServiceResult && dataPipelineService != null && logRecordErrorHandlerService != null) {
logRecordErrorHandlerService.dataPipelineErrorHandler();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cn.monitor4all.logRecord.util;


import cn.monitor4all.logRecord.bean.LogDTO;
import cn.monitor4all.logRecord.bean.LogRequest;
import cn.monitor4all.logRecord.context.LogRecordContext;
import cn.monitor4all.logRecord.handler.OperationLogHandler;
import cn.monitor4all.logRecord.thread.LogRecordThreadPool;
import com.alibaba.ttl.TtlRunnable;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Consumer;

/**
* 操作日志工具
*/
@Slf4j
public class OperationLogUtil {

private static final OperationLogHandler operationLogHandler = SpringContextUtil.getBean(OperationLogHandler.class);
private static final LogRecordThreadPool logRecordThreadPool = SpringContextUtil.getBean(LogRecordThreadPool.class);

/**
* 生成LogDTO并交由框架统一处理
*/
public static void log(LogRequest logRequest) {

try {
LogDTO logDTO = generateLogDTO(logRequest);
Consumer<LogDTO> createLogFunction = log -> operationLogHandler.createLog(log, logRequest.getExecutionTime());
if (logRecordThreadPool != null) {
Runnable task = () -> createLogFunction.accept(logDTO);
Runnable ttlRunnable = TtlRunnable.get(task);
logRecordThreadPool.getLogRecordPoolExecutor().execute(ttlRunnable);
} else {
operationLogHandler.createLog(logDTO, logRequest.getExecutionTime());
}
// 清除Context:每次方法执行一次
LogRecordContext.clearContext();
} catch (Throwable throwableFinal) {
log.error("OperationLogAspect send logDTO error", throwableFinal);
}
}

private static LogDTO generateLogDTO(LogRequest logRequest) {
LogDTO logDTO = new LogDTO();
logDTO.setBizId(logRequest.getBizId());
logDTO.setBizType(logRequest.getBizType());
logDTO.setException(logRequest.getException());
logDTO.setOperateDate(logRequest.getOperateDate());
logDTO.setSuccess(logRequest.getSuccess());
logDTO.setMsg(logRequest.getMsg());
logDTO.setTag(logRequest.getTag());
logDTO.setReturnStr(logRequest.getReturnStr());
logDTO.setExecutionTime(logRequest.getExecutionTime());
logDTO.setExtra(logRequest.getExtra());
logDTO.setOperatorId(logRequest.getOperatorId());
return logDTO;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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;

@Override
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}

public static <T> T getBean(Class<T> clazz) {
try {
return applicationContext.getBean(clazz);
} catch (BeansException e) {
log.info("Bean {} not existed, default return null.", clazz.getName());
return null;
}
}
}
Loading

0 comments on commit 57ba684

Please sign in to comment.