mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
增加组件请求日志打印配置
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package com.cf.imes.module.executor.service.plan.producing;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组件生产系统 HTTP 报文日志开关。
|
||||||
|
*
|
||||||
|
* <p>配置可通过配置中心动态刷新;缺省关闭成功请求及响应的完整报文日志,
|
||||||
|
* HTTP、业务和网络异常日志不受此开关影响。</p>
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@RefreshScope
|
||||||
|
@ConfigurationProperties(prefix = "chenfeng.producing")
|
||||||
|
public class ProducingHttpLogProperties {
|
||||||
|
|
||||||
|
/** 是否输出组件接口成功请求及响应的完整报文,缺省关闭。 */
|
||||||
|
private boolean httpLogEnabled = false;
|
||||||
|
}
|
||||||
+17
-8
@@ -27,12 +27,15 @@ import static com.cf.imes.module.executor.enums.ErrorCodeConstants.PRODUCING_PLA
|
|||||||
public class ProducingPlanOrderClient {
|
public class ProducingPlanOrderClient {
|
||||||
|
|
||||||
private final ProducingPlanOrderProperties properties;
|
private final ProducingPlanOrderProperties properties;
|
||||||
|
private final ProducingHttpLogProperties httpLogProperties;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final HttpClient httpClient;
|
private final HttpClient httpClient;
|
||||||
|
|
||||||
public ProducingPlanOrderClient(ProducingPlanOrderProperties properties,
|
public ProducingPlanOrderClient(ProducingPlanOrderProperties properties,
|
||||||
|
ProducingHttpLogProperties httpLogProperties,
|
||||||
ObjectMapper objectMapper) {
|
ObjectMapper objectMapper) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
|
this.httpLogProperties = httpLogProperties;
|
||||||
this.objectMapper = objectMapper;
|
this.objectMapper = objectMapper;
|
||||||
this.httpClient = HttpClient.newBuilder()
|
this.httpClient = HttpClient.newBuilder()
|
||||||
.connectTimeout(properties.getConnectTimeout())
|
.connectTimeout(properties.getConnectTimeout())
|
||||||
@@ -43,8 +46,10 @@ public class ProducingPlanOrderClient {
|
|||||||
List<ProducingPlanOrderMaterialGroup> customMaterialGroupList) {
|
List<ProducingPlanOrderMaterialGroup> customMaterialGroupList) {
|
||||||
CreateRequest request = new CreateRequest(
|
CreateRequest request = new CreateRequest(
|
||||||
blockIds, workflowSchemeId, customMaterialGroupList);
|
blockIds, workflowSchemeId, customMaterialGroupList);
|
||||||
log.info("Submitting producing plan order customMaterialGroupList={}",
|
if (httpLogProperties.isHttpLogEnabled()) {
|
||||||
serialize(customMaterialGroupList));
|
log.info("Submitting producing plan order customMaterialGroupList={}",
|
||||||
|
serialize(customMaterialGroupList));
|
||||||
|
}
|
||||||
String method = "POST";
|
String method = "POST";
|
||||||
String url = properties.getBaseUrl();
|
String url = properties.getBaseUrl();
|
||||||
String requestBody = serialize(request);
|
String requestBody = serialize(request);
|
||||||
@@ -217,8 +222,10 @@ public class ProducingPlanOrderClient {
|
|||||||
|
|
||||||
private HttpResponse<String> send(String url, String token, String method,
|
private HttpResponse<String> send(String url, String token, String method,
|
||||||
String body, String operation) {
|
String body, String operation) {
|
||||||
log.info("Submitting producing plan order operation {}, method={}, url={}, requestBody={}",
|
if (httpLogProperties.isHttpLogEnabled()) {
|
||||||
operation, method, url, body == null ? "" : body);
|
log.info("Submitting producing plan order operation {}, method={}, url={}, requestBody={}",
|
||||||
|
operation, method, url, body == null ? "" : body);
|
||||||
|
}
|
||||||
HttpRequest.BodyPublisher publisher = body == null
|
HttpRequest.BodyPublisher publisher = body == null
|
||||||
? HttpRequest.BodyPublishers.noBody()
|
? HttpRequest.BodyPublishers.noBody()
|
||||||
: HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8);
|
: HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8);
|
||||||
@@ -232,10 +239,12 @@ public class ProducingPlanOrderClient {
|
|||||||
try {
|
try {
|
||||||
HttpResponse<String> response = httpClient.send(
|
HttpResponse<String> response = httpClient.send(
|
||||||
request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
|
||||||
log.info("Producing plan order operation {} response, method={}, url={}, "
|
if (httpLogProperties.isHttpLogEnabled()) {
|
||||||
+ "httpStatus={}, responseBody={}",
|
log.info("Producing plan order operation {} response, method={}, url={}, "
|
||||||
operation, method, url, response.statusCode(),
|
+ "httpStatus={}, responseBody={}",
|
||||||
responseBodyForLog(response.body()));
|
operation, method, url, response.statusCode(),
|
||||||
|
responseBodyForLog(response.body()));
|
||||||
|
}
|
||||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||||
logRequestFailure(operation, method, url, body,
|
logRequestFailure(operation, method, url, body,
|
||||||
response.statusCode(), response.body(), null);
|
response.statusCode(), response.body(), null);
|
||||||
|
|||||||
+2
-1
@@ -33,7 +33,8 @@ class ProducingPlanOrderClientTest {
|
|||||||
ProducingPlanOrderProperties properties = new ProducingPlanOrderProperties();
|
ProducingPlanOrderProperties properties = new ProducingPlanOrderProperties();
|
||||||
properties.setBaseUrl("http://localhost:" + server.getAddress().getPort()
|
properties.setBaseUrl("http://localhost:" + server.getAddress().getPort()
|
||||||
+ "/api/v1/producing/plan-order");
|
+ "/api/v1/producing/plan-order");
|
||||||
client = new ProducingPlanOrderClient(properties, objectMapper);
|
client = new ProducingPlanOrderClient(
|
||||||
|
properties, new ProducingHttpLogProperties(), objectMapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
|
|||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
package com.cf.imes.module.plan.service.orderImport.producing;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组件生产系统 HTTP 报文日志开关。
|
||||||
|
*
|
||||||
|
* <p>配置可通过配置中心动态刷新;缺省关闭成功请求及响应的完整报文日志,
|
||||||
|
* HTTP、业务和网络异常日志不受此开关影响。</p>
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@RefreshScope
|
||||||
|
@ConfigurationProperties(prefix = "chenfeng.producing")
|
||||||
|
public class ProducingHttpLogProperties {
|
||||||
|
|
||||||
|
/** 是否输出组件接口成功请求及响应的完整报文,缺省关闭。 */
|
||||||
|
private boolean httpLogEnabled = false;
|
||||||
|
}
|
||||||
+15
-5
@@ -35,6 +35,7 @@ import static com.cf.imes.module.plan.enums.ErrorCodeConstants.PRODUCING_ORDER_S
|
|||||||
public class ProducingOrderClient {
|
public class ProducingOrderClient {
|
||||||
|
|
||||||
private final ProducingOrderImportProperties properties;
|
private final ProducingOrderImportProperties properties;
|
||||||
|
private final ProducingHttpLogProperties httpLogProperties;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final HttpClient httpClient;
|
private final HttpClient httpClient;
|
||||||
|
|
||||||
@@ -42,10 +43,14 @@ public class ProducingOrderClient {
|
|||||||
* 创建组件生产系统客户端,并从配置初始化超时参数。
|
* 创建组件生产系统客户端,并从配置初始化超时参数。
|
||||||
*
|
*
|
||||||
* @param properties 组件生产单接口配置
|
* @param properties 组件生产单接口配置
|
||||||
|
* @param httpLogProperties 组件接口成功报文日志开关
|
||||||
* @param objectMapper 系统 Jackson 配置
|
* @param objectMapper 系统 Jackson 配置
|
||||||
*/
|
*/
|
||||||
public ProducingOrderClient(ProducingOrderImportProperties properties, ObjectMapper objectMapper) {
|
public ProducingOrderClient(ProducingOrderImportProperties properties,
|
||||||
|
ProducingHttpLogProperties httpLogProperties,
|
||||||
|
ObjectMapper objectMapper) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
|
this.httpLogProperties = httpLogProperties;
|
||||||
this.objectMapper = objectMapper.copy()
|
this.objectMapper = objectMapper.copy()
|
||||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
this.httpClient = HttpClient.newBuilder()
|
this.httpClient = HttpClient.newBuilder()
|
||||||
@@ -220,6 +225,9 @@ public class ProducingOrderClient {
|
|||||||
int attempt,
|
int attempt,
|
||||||
int maxAttempts,
|
int maxAttempts,
|
||||||
String requestBody) {
|
String requestBody) {
|
||||||
|
if (!httpLogProperties.isHttpLogEnabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
log.info("[producing-order-http-request] operation={} method={} uri={} attempt={}/{} "
|
log.info("[producing-order-http-request] operation={} method={} uri={} attempt={}/{} "
|
||||||
+ "requestBytes={} requestBody={}",
|
+ "requestBytes={} requestBody={}",
|
||||||
operation, method, uri, attempt, maxAttempts,
|
operation, method, uri, attempt, maxAttempts,
|
||||||
@@ -238,10 +246,12 @@ public class ProducingOrderClient {
|
|||||||
long elapsedMs,
|
long elapsedMs,
|
||||||
String responseBody) {
|
String responseBody) {
|
||||||
if (status >= 200 && status < 300) {
|
if (status >= 200 && status < 300) {
|
||||||
log.info("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
if (httpLogProperties.isHttpLogEnabled()) {
|
||||||
+ "status={} elapsedMs={} responseBytes={} result={}",
|
log.info("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
||||||
operation, method, uri, attempt, maxAttempts, status, elapsedMs,
|
+ "status={} elapsedMs={} responseBytes={} result={}",
|
||||||
utf8Length(responseBody), payload(responseBody));
|
operation, method, uri, attempt, maxAttempts, status, elapsedMs,
|
||||||
|
utf8Length(responseBody), payload(responseBody));
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log.error("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
log.error("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
|
||||||
|
|||||||
Reference in New Issue
Block a user