增加组件请求日志打印配置

This commit is contained in:
gaoqr
2026-08-05 17:09:33 +08:00
parent 2092a45a87
commit ed4123f502
5 changed files with 78 additions and 14 deletions
@@ -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;
}
@@ -27,12 +27,15 @@ import static com.cf.imes.module.executor.enums.ErrorCodeConstants.PRODUCING_PLA
public class ProducingPlanOrderClient {
private final ProducingPlanOrderProperties properties;
private final ProducingHttpLogProperties httpLogProperties;
private final ObjectMapper objectMapper;
private final HttpClient httpClient;
public ProducingPlanOrderClient(ProducingPlanOrderProperties properties,
ProducingHttpLogProperties httpLogProperties,
ObjectMapper objectMapper) {
this.properties = properties;
this.httpLogProperties = httpLogProperties;
this.objectMapper = objectMapper;
this.httpClient = HttpClient.newBuilder()
.connectTimeout(properties.getConnectTimeout())
@@ -43,8 +46,10 @@ public class ProducingPlanOrderClient {
List<ProducingPlanOrderMaterialGroup> customMaterialGroupList) {
CreateRequest request = new CreateRequest(
blockIds, workflowSchemeId, customMaterialGroupList);
log.info("Submitting producing plan order customMaterialGroupList={}",
serialize(customMaterialGroupList));
if (httpLogProperties.isHttpLogEnabled()) {
log.info("Submitting producing plan order customMaterialGroupList={}",
serialize(customMaterialGroupList));
}
String method = "POST";
String url = properties.getBaseUrl();
String requestBody = serialize(request);
@@ -217,8 +222,10 @@ public class ProducingPlanOrderClient {
private HttpResponse<String> send(String url, String token, String method,
String body, String operation) {
log.info("Submitting producing plan order operation {}, method={}, url={}, requestBody={}",
operation, method, url, body == null ? "" : body);
if (httpLogProperties.isHttpLogEnabled()) {
log.info("Submitting producing plan order operation {}, method={}, url={}, requestBody={}",
operation, method, url, body == null ? "" : body);
}
HttpRequest.BodyPublisher publisher = body == null
? HttpRequest.BodyPublishers.noBody()
: HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8);
@@ -232,10 +239,12 @@ public class ProducingPlanOrderClient {
try {
HttpResponse<String> response = httpClient.send(
request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
log.info("Producing plan order operation {} response, method={}, url={}, "
+ "httpStatus={}, responseBody={}",
operation, method, url, response.statusCode(),
responseBodyForLog(response.body()));
if (httpLogProperties.isHttpLogEnabled()) {
log.info("Producing plan order operation {} response, method={}, url={}, "
+ "httpStatus={}, responseBody={}",
operation, method, url, response.statusCode(),
responseBodyForLog(response.body()));
}
if (response.statusCode() < 200 || response.statusCode() >= 300) {
logRequestFailure(operation, method, url, body,
response.statusCode(), response.body(), null);
@@ -33,7 +33,8 @@ class ProducingPlanOrderClientTest {
ProducingPlanOrderProperties properties = new ProducingPlanOrderProperties();
properties.setBaseUrl("http://localhost:" + server.getAddress().getPort()
+ "/api/v1/producing/plan-order");
client = new ProducingPlanOrderClient(properties, objectMapper);
client = new ProducingPlanOrderClient(
properties, new ProducingHttpLogProperties(), objectMapper);
}
@AfterEach