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

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.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;
}
@@ -35,6 +35,7 @@ import static com.cf.imes.module.plan.enums.ErrorCodeConstants.PRODUCING_ORDER_S
public class ProducingOrderClient {
private final ProducingOrderImportProperties properties;
private final ProducingHttpLogProperties httpLogProperties;
private final ObjectMapper objectMapper;
private final HttpClient httpClient;
@@ -42,10 +43,14 @@ public class ProducingOrderClient {
* 创建组件生产系统客户端,并从配置初始化超时参数。
*
* @param properties 组件生产单接口配置
* @param httpLogProperties 组件接口成功报文日志开关
* @param objectMapper 系统 Jackson 配置
*/
public ProducingOrderClient(ProducingOrderImportProperties properties, ObjectMapper objectMapper) {
public ProducingOrderClient(ProducingOrderImportProperties properties,
ProducingHttpLogProperties httpLogProperties,
ObjectMapper objectMapper) {
this.properties = properties;
this.httpLogProperties = httpLogProperties;
this.objectMapper = objectMapper.copy()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.httpClient = HttpClient.newBuilder()
@@ -220,6 +225,9 @@ public class ProducingOrderClient {
int attempt,
int maxAttempts,
String requestBody) {
if (!httpLogProperties.isHttpLogEnabled()) {
return;
}
log.info("[producing-order-http-request] operation={} method={} uri={} attempt={}/{} "
+ "requestBytes={} requestBody={}",
operation, method, uri, attempt, maxAttempts,
@@ -238,10 +246,12 @@ public class ProducingOrderClient {
long elapsedMs,
String responseBody) {
if (status >= 200 && status < 300) {
log.info("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
+ "status={} elapsedMs={} responseBytes={} result={}",
operation, method, uri, attempt, maxAttempts, status, elapsedMs,
utf8Length(responseBody), payload(responseBody));
if (httpLogProperties.isHttpLogEnabled()) {
log.info("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "
+ "status={} elapsedMs={} responseBytes={} result={}",
operation, method, uri, attempt, maxAttempts, status, elapsedMs,
utf8Length(responseBody), payload(responseBody));
}
return;
}
log.error("[producing-order-http-result] operation={} method={} uri={} attempt={}/{} "