-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from qqxx6661/dev_1.7.x
manual log support / 支持静态类(非注解方式)记录日志
- Loading branch information
Showing
33 changed files
with
485 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
log-record-core/src/main/java/cn/monitor4all/logRecord/bean/LogRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
77 changes: 77 additions & 0 deletions
77
log-record-core/src/main/java/cn/monitor4all/logRecord/handler/OperationLogHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
log-record-core/src/main/java/cn/monitor4all/logRecord/util/OperationLogUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
log-record-core/src/main/java/cn/monitor4all/logRecord/util/SpringContextUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.