修改请求日志输出所有相应数据导致日志太大的问题

main
jiacheng.deng 2022-12-16 16:40:42 +08:00
parent 88d6b40fb5
commit e99562c417
3 changed files with 86 additions and 34 deletions

View File

@ -27,6 +27,8 @@ public class ResponeVo implements HttpResponse {
* *
*/ */
private String entityString; private String entityString;
private Map<String, Object> entityMap;
/** /**
* *
*/ */
@ -39,7 +41,6 @@ public class ResponeVo implements HttpResponse {
private Map<String, Object> requestData; private Map<String, Object> requestData;
private HttpResponse response; private HttpResponse response;
public int getCode() { public int getCode() {
@ -68,11 +69,15 @@ public class ResponeVo implements HttpResponse {
* @return map * @return map
* @throws JsonProcessingException JSON * @throws JsonProcessingException JSON
*/ */
@Deprecated
public Map<String, Object> getEntityMap() throws JsonProcessingException { public Map<String, Object> getEntityMap() throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(this.getEntityString(), Map.class); return mapper.readValue(this.getEntityString(), Map.class);
} }
public Map<String, Object> getResponseMap(){
return this.entityMap;
}
/** /**
* *
* *
@ -81,11 +86,21 @@ public class ResponeVo implements HttpResponse {
* @return * @return
* @throws JsonProcessingException JSON * @throws JsonProcessingException JSON
*/ */
@Deprecated
public <T> T getEntity(Class<T> clazz) throws JsonProcessingException { public <T> T getEntity(Class<T> clazz) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(this.getEntityString(), clazz); return mapper.readValue(this.getEntityString(), clazz);
} }
public <T> T getResponseEntity(Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(this.getEntityString(), clazz);
} catch (JsonProcessingException e) {
return null;
}
}
/** /**
* *
* *
@ -98,6 +113,45 @@ public class ResponeVo implements HttpResponse {
return mapper.readValue(this.getEntityString(), typeReference); return mapper.readValue(this.getEntityString(), typeReference);
} }
public String getEntityString() {
return entityString;
}
public void setEntityString(String entityString) {
this.entityString = entityString;
try{
ObjectMapper mapper = new ObjectMapper();
this.entityMap = mapper.readValue(this.getEntityString(), Map.class);
}catch (Exception ignore){
}
}
public InputStream getContent() {
return content;
}
public void setContent(InputStream content) {
this.content = content;
}
public byte[] getContentByteArr() {
return contentByteArr;
}
public void setContentByteArr(byte[] contentByteArr) {
this.contentByteArr = contentByteArr;
}
@Override
public String toString() {
return "ResponeVo{" +
"code=" + code +
", entityString='" + entityString + '\'' +
", otherParam=" + requestData +
'}';
}
/** /**
* *
* *
@ -243,37 +297,6 @@ public class ResponeVo implements HttpResponse {
} }
public String getEntityString() {
return entityString;
}
public void setEntityString(String entityString) {
this.entityString = entityString;
}
public InputStream getContent() {
return content;
}
public void setContent(InputStream content) {
this.content = content;
}
public byte[] getContentByteArr() {
return contentByteArr;
}
public void setContentByteArr(byte[] contentByteArr) {
this.contentByteArr = contentByteArr;
}
@Override
public String toString() {
return "ResponeVo{" +
"code=" + code +
", entityString='" + entityString + '\'' +
", otherParam=" + requestData +
'}';
}
} }

View File

@ -5,6 +5,7 @@ import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data; import lombok.Data;
import java.util.Date; import java.util.Date;
import java.util.Map;
/** /**
* <h1>http</h1> * <h1>http</h1>
@ -27,5 +28,7 @@ public class HttpUtilParamInfo {
@JSONField(format = "yyyy-MM-dd HH:mm:ss") @JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date responseDate; private Date responseDate;
private Map<String,Object> responseMap;
private String responseString;
private ResponeVo response; private ResponeVo response;
} }

View File

@ -8,9 +8,9 @@ import aiyh.utils.zwl.common.ToolUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsProvider;
@ -57,6 +57,8 @@ public class HttpUtils {
* basic * basic
*/ */
private CredentialsProvider credentialsProvider = null; private CredentialsProvider credentialsProvider = null;
private final PropertyPreFilters filters = new PropertyPreFilters();
private final PropertyPreFilters.MySimplePropertyPreFilter excludefilter = filters.addFilter();
{ {
// private final ExecutorService executorService = Executors.newFixedThreadPool(3); // private final ExecutorService executorService = Executors.newFixedThreadPool(3);
@ -67,6 +69,8 @@ public class HttpUtils {
new LinkedBlockingQueue<>(1024), new LinkedBlockingQueue<>(1024),
threadFactory, threadFactory,
new ThreadPoolExecutor.AbortPolicy()); new ThreadPoolExecutor.AbortPolicy());
String[] excludeProperties = {"locale","contentByteArr","response"};
excludefilter.addExcludes(excludeProperties);
} }
public HttpUtils() { public HttpUtils() {
@ -638,10 +642,16 @@ public class HttpUtils {
httpUtilParamInfo = new HttpUtilParamInfo(); httpUtilParamInfo = new HttpUtilParamInfo();
} }
httpUtilParamInfo.setResponse(apply); httpUtilParamInfo.setResponse(apply);
if(apply.getResponseMap() == null){
httpUtilParamInfo.setResponseString(apply.getEntityString());
}else {
httpUtilParamInfo.setResponseMap(apply.getResponseMap());
}
httpUtilParamInfo.setResponseDate(new Date()); httpUtilParamInfo.setResponseDate(new Date());
try { try {
log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(),
JSONObject.toJSONString(httpUtilParamInfo, JSONObject.toJSONString(httpUtilParamInfo,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat))); SerializerFeature.WriteDateUseDateFormat)));
} catch (Exception ignore) { } catch (Exception ignore) {
@ -669,11 +679,17 @@ public class HttpUtils {
httpUtilParamInfo = new HttpUtilParamInfo(); httpUtilParamInfo = new HttpUtilParamInfo();
} }
httpUtilParamInfo.setResponse(apply); httpUtilParamInfo.setResponse(apply);
if(apply.getResponseMap() == null){
httpUtilParamInfo.setResponseString(apply.getEntityString());
}else {
httpUtilParamInfo.setResponseMap(apply.getResponseMap());
}
httpUtilParamInfo.setResponseDate(new Date()); httpUtilParamInfo.setResponseDate(new Date());
HTTP_UTIL_PARAM_INFO_THREAD_LOCAL.remove(); HTTP_UTIL_PARAM_INFO_THREAD_LOCAL.remove();
try { try {
log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(),
JSONObject.toJSONString(httpUtilParamInfo, JSONObject.toJSONString(httpUtilParamInfo,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat))); SerializerFeature.WriteDateUseDateFormat)));
} catch (Exception ignore) { } catch (Exception ignore) {
@ -696,17 +712,22 @@ public class HttpUtils {
try { try {
response = httpClient.execute(request); response = httpClient.execute(request);
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity();
Header[] allHeaders = response.getAllHeaders();
Locale locale = response.getLocale(); Locale locale = response.getLocale();
responeVo.setLocale(locale); responeVo.setLocale(locale);
responeVo.setEntityString(EntityUtils.toString(entity, DEFAULT_ENCODING)); responeVo.setEntityString(EntityUtils.toString(entity, DEFAULT_ENCODING));
responeVo.setCode(response.getStatusLine().getStatusCode()); responeVo.setCode(response.getStatusLine().getStatusCode());
responeVo.setResponse(response); responeVo.setResponse(response);
httpUtilParamInfo.setResponse(responeVo); httpUtilParamInfo.setResponse(responeVo);
if(responeVo.getResponseMap() == null){
httpUtilParamInfo.setResponseString(responeVo.getEntityString());
}else {
httpUtilParamInfo.setResponseMap(responeVo.getResponseMap());
}
httpUtilParamInfo.setResponseDate(new Date()); httpUtilParamInfo.setResponseDate(new Date());
try { try {
log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(),
JSONObject.toJSONString(httpUtilParamInfo, JSONObject.toJSONString(httpUtilParamInfo,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat))); SerializerFeature.WriteDateUseDateFormat)));
} catch (Exception ignore) { } catch (Exception ignore) {
@ -718,6 +739,7 @@ public class HttpUtils {
httpUtilParamInfo.setResponseDate(new Date()); httpUtilParamInfo.setResponseDate(new Date());
log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(), log.info(Util.logStr("url [{}] request info : [\n{}\n];", httpUtilParamInfo.getUrl(),
JSONObject.toJSONString(httpUtilParamInfo, JSONObject.toJSONString(httpUtilParamInfo,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat))); SerializerFeature.WriteDateUseDateFormat)));
} catch (Exception ignore) { } catch (Exception ignore) {
@ -1021,9 +1043,11 @@ public class HttpUtils {
private HttpPost uploadFileByInputStream(String url, List<HttpMultipartFile> multipartFileList, private HttpPost uploadFileByInputStream(String url, List<HttpMultipartFile> multipartFileList,
Map<String, Object> params, Map<String, String> headers) { Map<String, Object> params, Map<String, String> headers) {
log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params, log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat), SerializerFeature.WriteDateUseDateFormat),
JSONObject.toJSONString(headers, JSONObject.toJSONString(headers,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat))); SerializerFeature.WriteDateUseDateFormat)));
HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo(); HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo();
@ -1074,9 +1098,11 @@ public class HttpUtils {
private HttpPut uploadFileByInputStreamPut(String url, List<HttpMultipartFile> multipartFileList, private HttpPut uploadFileByInputStreamPut(String url, List<HttpMultipartFile> multipartFileList,
Map<String, Object> params, Map<String, String> headers) { Map<String, Object> params, Map<String, String> headers) {
log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params, log.info(Util.logStr("start request : url is [{}],other param [\n{}\n],heard [\n{}\n]", url, JSONObject.toJSONString(params,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat), SerializerFeature.WriteDateUseDateFormat),
JSONObject.toJSONString(headers, JSONObject.toJSONString(headers,
excludefilter,
SerializerFeature.PrettyFormat, SerializerFeature.PrettyFormat,
SerializerFeature.WriteDateUseDateFormat))); SerializerFeature.WriteDateUseDateFormat)));
HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo(); HttpUtilParamInfo httpUtilParamInfo = new HttpUtilParamInfo();