Skip to content

Commit

Permalink
feat: manual operation log
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxx6661 committed May 28, 2024
1 parent a38bae4 commit 058341e
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 81 deletions.
38 changes: 19 additions & 19 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-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down 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
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,20 @@
package cn.monitor4all.logRecord.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
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) {
return applicationContext.getBean(clazz);
}
}
4 changes: 2 additions & 2 deletions log-record-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-starter</artifactId>
<version>1.6.3</version>
<version>1.7.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</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
Loading

0 comments on commit 058341e

Please sign in to comment.