update - 调整API 及 Service 日志记录

This commit is contained in:
wangli 2024-04-07 11:12:49 +08:00
parent 0bd3403b95
commit fc6ac8f310
6 changed files with 71 additions and 34 deletions

View File

@ -11,23 +11,25 @@ import org.springframework.context.ApplicationEvent;
*/
@Slf4j
public class ApiLogEvent extends ApplicationEvent {
private String uniqueId;
private String traceId;
private String apiUrl;
private Object requestBody;
private Object responseBody;
private Double takeTime;
private String type;
public ApiLogEvent(String uniqueId, String apiUrl, Object requestBody, Object responseBody, Double takeTime) {
super(uniqueId);
public ApiLogEvent(String traceId, String apiUrl, Object requestBody, Object responseBody, Double takeTime, String type) {
super(traceId);
this.traceId = traceId;
this.apiUrl = apiUrl;
this.requestBody = requestBody;
this.responseBody = responseBody;
this.takeTime = takeTime;
this.type = type;
}
public String getUniqueId() {
return uniqueId;
public String getTraceId() {
return traceId;
}
public String getApiUrl() {
@ -45,4 +47,8 @@ public class ApiLogEvent extends ApplicationEvent {
public Double getTakeTime() {
return takeTime;
}
public String getType() {
return type;
}
}

View File

@ -37,11 +37,12 @@ public class ApiLogListener implements ApplicationListener<ApiLogEvent> {
return;
}
ExtAxApiLog apiLog = new ExtAxApiLog();
apiLog.setUniqueId(event.getUniqueId());
apiLog.setTraceId(event.getTraceId());
apiLog.setApiUrl(event.getApiUrl());
apiLog.setRequestBody(JSON.toJSONString(event.getRequestBody()));
apiLog.setResponseBody(JSON.toJSONString(event.getResponseBody()));
apiLog.setTakeTime(event.getTakeTime());
apiLog.setType(event.getType());
apiLogService.insert(apiLog);
}
}

View File

@ -19,9 +19,9 @@ import lombok.ToString;
public class ExtAxApiLog extends BaseEntity<ExtAxDict> {
/**
* 唯一标识
* 日志跟踪 ID
*/
private String uniqueId;
private String traceId;
/**
* 请求 API
@ -43,4 +43,8 @@ public class ExtAxApiLog extends BaseEntity<ExtAxDict> {
*/
private Double takeTime;
/**
* 函数类型
*/
private String type;
}

View File

@ -2,11 +2,12 @@ DROP TABLE IF EXISTS `EXT_AX_API_LOG`;
CREATE TABLE IF NOT EXISTS EXT_AX_API_LOG
(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
`unique_id` varchar(100) NOT NULL DEFAULT '' COMMENT '唯一标识',
`trace_id` varchar(100) NOT NULL DEFAULT '' COMMENT '唯一标识',
`api_url` varchar(2000) NOT NULL DEFAULT '' COMMENT '请求 API',
`request_body` text NOT NULL COMMENT '请求参数',
`response_body` text NOT NULL COMMENT '响应参数',
`take_time` decimal(5, 2) NOT NULL COMMENT '执行耗时',
`type` varchar(10) NOT NULL DEFAULT '' COMMENT '函数类型',
`create_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`is_delete` bigint NOT NULL DEFAULT '0' COMMENT '是否删除',

View File

@ -1,19 +0,0 @@
package cn.axzo.workflow.server.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* Controller 方法执行耗时
*
* @author wangli
* @since 2024/1/18 18:02
*/
@Aspect
@Component
public class ControllerTimingAspect {
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
public void controllerMethods() {}
}

View File

@ -3,7 +3,6 @@ package cn.axzo.workflow.server.common.aspectj;
import cn.axzo.workflow.core.common.event.ApiLogEvent;
import cn.axzo.workflow.server.common.annotation.EnvConfig;
import cn.axzo.workflow.server.common.annotation.ErrorReporter;
import cn.hutool.core.util.IdUtil;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
@ -12,14 +11,19 @@ import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Arrays;
import static cn.azxo.framework.common.constatns.Constants.CTX_LOG_ID_MDC;
/**
* 异常错误信息报告器
*
@ -44,20 +48,60 @@ public class ErrorReportAspect {
*
* @param joinPoint 切点
*/
@Around("@within(org.springframework.web.bind.annotation.RestController)")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
@Around("@within(org.springframework.web.bind.annotation.RestController) || @within(org.springframework.stereotype.Service)")
public Object doControllerAround(ProceedingJoinPoint joinPoint) throws Throwable {
getTargetAnnotation(joinPoint);
Signature signature = joinPoint.getSignature();
StopWatch watch = new StopWatch("running task time");
StopWatch watch = new StopWatch("running controller time");
watch.start(signature.toShortString());
Object result = joinPoint.proceed();
watch.stop();
log.info("StopWatch '" + watch.getLastTaskName() + "': running time = " + watch.getTotalTimeSeconds() + " s");
ApiLogEvent event = new ApiLogEvent(IdUtil.fastSimpleUUID(), signature.toShortString(), joinPoint.getArgs(), result, watch.getTotalTimeSeconds());
ApiLogEvent event = new ApiLogEvent(MDC.get(CTX_LOG_ID_MDC),
signature.toShortString(),
joinPoint.getArgs(),
result,
watch.getTotalTimeSeconds(),
getType(joinPoint));
applicationEventPublisher.publishEvent(event);
return result;
}
private String getType(ProceedingJoinPoint joinPoint) {
if (isController(joinPoint)) {
return "Controller";
} else if (isService(joinPoint)) {
return "Service";
} else {
return "";
}
}
private Boolean isController(ProceedingJoinPoint joinPoint) {
return getTargetAnnotation(joinPoint).isAnnotationPresent(RestController.class);
}
private Boolean isService(ProceedingJoinPoint joinPoint) {
return getTargetAnnotation(joinPoint).isAnnotationPresent(Service.class);
}
private Class getTargetAnnotation(ProceedingJoinPoint joinPoint) {
return joinPoint.getTarget().getClass();
}
@Around("@within(org.springframework.stereotype.Service)")
public Object doServiceAround(ProceedingJoinPoint joinPoint) throws Throwable {
Signature signature = joinPoint.getSignature();
StopWatch watch = new StopWatch("running service time");
watch.start(signature.toShortString());
Object result = joinPoint.proceed();
watch.stop();
return result;
}
/**
* 拦截异常操作
*