update - API log 日志记录,增加调用方和客户端版本
This commit is contained in:
parent
f2b49ebfbc
commit
2bebe80382
@ -13,15 +13,20 @@ import org.springframework.context.ApplicationEvent;
|
||||
public class ApiLogEvent extends ApplicationEvent {
|
||||
private String traceId;
|
||||
private String apiUrl;
|
||||
private String requestApplicationName;
|
||||
private String clientVersion;
|
||||
private Object requestBody;
|
||||
private Object responseBody;
|
||||
private Double takeTime;
|
||||
private String type;
|
||||
|
||||
public ApiLogEvent(String traceId, String apiUrl, Object requestBody, Object responseBody, Double takeTime, String type) {
|
||||
public ApiLogEvent(String traceId, String apiUrl, String requestApplicationName, String clientVersion,
|
||||
Object requestBody, Object responseBody, Double takeTime, String type) {
|
||||
super(apiUrl);
|
||||
this.traceId = traceId;
|
||||
this.apiUrl = apiUrl;
|
||||
this.requestApplicationName = requestApplicationName;
|
||||
this.clientVersion = clientVersion;
|
||||
this.requestBody = requestBody;
|
||||
this.responseBody = responseBody;
|
||||
this.takeTime = takeTime;
|
||||
@ -40,6 +45,14 @@ public class ApiLogEvent extends ApplicationEvent {
|
||||
return apiUrl;
|
||||
}
|
||||
|
||||
public String getRequestApplicationName() {
|
||||
return requestApplicationName;
|
||||
}
|
||||
|
||||
public String getClientVersion() {
|
||||
return clientVersion;
|
||||
}
|
||||
|
||||
public Object getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
@ -28,6 +28,16 @@ public class ExtAxApiLog extends BaseEntity<ExtAxApiLog> {
|
||||
*/
|
||||
private String apiUrl;
|
||||
|
||||
/**
|
||||
* 请求方服务名称
|
||||
*/
|
||||
private String requestApplicationName;
|
||||
|
||||
/**
|
||||
* 客户端 API 版本
|
||||
*/
|
||||
private String clientVersion;
|
||||
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
alter table ext_ax_api_log
|
||||
modify request_body longblob null comment '请求参数';
|
||||
|
||||
alter table ext_ax_api_log
|
||||
add request_application_name varchar(20) default '' null comment '请求方服务名称' after type;
|
||||
|
||||
alter table ext_ax_api_log
|
||||
add client_version varchar(20) default '' null comment '客户端 API 版本' after request_application_name;
|
||||
|
||||
|
||||
@ -20,11 +20,16 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.axzo.workflow.client.config.WorkflowRequestInterceptor.HEADER_API_VERSION;
|
||||
import static cn.axzo.workflow.client.config.WorkflowRequestInterceptor.HEADER_SERVER_NAME;
|
||||
import static cn.azxo.framework.common.constatns.Constants.CTX_LOG_ID_MDC;
|
||||
|
||||
/**
|
||||
@ -72,6 +77,8 @@ public class ErrorReportAspect implements Ordered {
|
||||
String type = getType(joinPoint);
|
||||
ApiLogEvent event = new ApiLogEvent(MDC.get(CTX_LOG_ID_MDC),
|
||||
signature.toShortString(),
|
||||
Objects.isNull(getOriginRequest()) ? "" : getOriginRequest().getHeader(HEADER_SERVER_NAME),
|
||||
Objects.isNull(getOriginRequest()) ? "" : getOriginRequest().getHeader(HEADER_API_VERSION),
|
||||
Objects.equals(type, "Controller") ? joinPoint.getArgs() : null,
|
||||
Objects.equals(type, "Controller") ? result : null,
|
||||
watch.getTotalTimeSeconds(),
|
||||
@ -127,4 +134,13 @@ public class ErrorReportAspect implements Ordered {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private HttpServletRequest getOriginRequest() {
|
||||
try {
|
||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return Objects.requireNonNull(requestAttributes).getRequest();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user