diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index e35d0b25..f8d915f8 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -88,6 +88,13 @@ public class WxPayConfig { * 默认不使用 */ private boolean useSandboxEnv = false; + + /** + * 是否将接口请求日志信息保存到threadLocal中. + * 默认不保存 + */ + private boolean ifSaveApiData = false; + private String httpProxyHost; private Integer httpProxyPort; private String httpProxyUsername; diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java index 753652f4..df9a82fb 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceApacheHttpImpl.java @@ -1,8 +1,9 @@ package com.github.binarywang.wxpay.service.impl; -import com.github.binarywang.wxpay.bean.WxPayApiData; -import com.github.binarywang.wxpay.exception.WxPayException; -import jodd.util.Base64; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import javax.net.ssl.SSLContext; + import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; @@ -20,9 +21,9 @@ import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; -import javax.net.ssl.SSLContext; -import java.io.UnsupportedEncodingException; -import java.nio.charset.StandardCharsets; +import com.github.binarywang.wxpay.bean.WxPayApiData; +import com.github.binarywang.wxpay.exception.WxPayException; +import jodd.util.Base64; /** *
@@ -65,7 +66,9 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
         try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
           String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
           this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
-          wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+          if (this.getConfig().isIfSaveApiData()) {
+            wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+          }
           return responseString;
         }
       } finally {
@@ -73,7 +76,9 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
       }
     } catch (Exception e) {
       this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
-      wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
+      if (this.getConfig().isIfSaveApiData()) {
+        wxApiData.set(new WxPayApiData(url, requestStr, null, e.getMessage()));
+      }
       throw new WxPayException(e.getMessage(), e);
     }
   }
diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java
index 1c7b315d..a5988099 100644
--- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java
+++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/WxPayServiceJoddHttpImpl.java
@@ -30,7 +30,9 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
       byte[] responseBytes = request.send().bodyBytes();
       final String responseString = Base64.encodeToString(responseBytes);
       this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseString);
-      wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+      if (this.getConfig().isIfSaveApiData()) {
+        wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+      }
       return responseBytes;
     } catch (Exception e) {
       this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
@@ -46,7 +48,9 @@ public class WxPayServiceJoddHttpImpl extends BaseWxPayServiceImpl {
       String responseString = this.getResponseString(request.send());
 
       this.log.info("\n【请求地址】:{}\n【请求数据】:{}\n【响应数据】:{}", url, requestStr, responseString);
-      wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+      if (this.getConfig().isIfSaveApiData()) {
+        wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
+      }
       return responseString;
     } catch (Exception e) {
       this.log.error("\n【请求地址】:{}\n【请求数据】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());