Improve external API logging readability

This commit is contained in:
wkc
2026-05-06 10:18:28 +08:00
parent 709a314107
commit abc8b127e1
7 changed files with 68 additions and 9 deletions

View File

@@ -21,6 +21,8 @@ import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.http.*;
@@ -75,7 +77,7 @@ public class HttpUtils
try
{
String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
log.info("sendGet - {}", urlNameString);
log.info("后端外部接口调用开始\n请求URL{}\n请求参数\n{}", urlNameString, formatLogValue(param));
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
@@ -88,7 +90,8 @@ public class HttpUtils
{
result.append(line);
}
log.info("recv - {}", result);
log.info("后端外部接口调用完成\n请求URL{}\n请求参数\n{}\n返回参数\n{}",
urlNameString, formatLogValue(param), formatLogValue(result));
}
catch (ConnectException e)
{
@@ -150,7 +153,7 @@ public class HttpUtils
StringBuilder result = new StringBuilder();
try
{
log.info("sendPost - {}", url);
log.info("后端外部接口调用开始\n请求URL{}\n请求参数\n{}", url, formatLogValue(param));
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
@@ -169,7 +172,8 @@ public class HttpUtils
{
result.append(line);
}
log.info("recv - {}", result);
log.info("后端外部接口调用完成\n请求URL{}\n请求参数\n{}\n返回参数\n{}",
url, formatLogValue(param), formatLogValue(result));
}
catch (ConnectException e)
{
@@ -219,7 +223,7 @@ public class HttpUtils
String urlNameString = url + "?" + param;
try
{
log.info("sendSSLPost - {}", urlNameString);
log.info("后端外部接口调用开始\n请求URL{}\n请求参数\n{}", urlNameString, formatLogValue(param));
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
URL console = new URL(urlNameString);
@@ -245,7 +249,8 @@ public class HttpUtils
result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}
}
log.info("recv - {}", result);
log.info("后端外部接口调用完成\n请求URL{}\n请求参数\n{}\n返回参数\n{}",
urlNameString, formatLogValue(param), formatLogValue(result));
conn.disconnect();
br.close();
}
@@ -327,7 +332,8 @@ public class HttpUtils
requestEntity,
responseType
);
log.info("---------------------->POST(form-urlencoded) 请求成功URL{},响应结果:{}", url, response.getBody());
log.info("后端外部接口调用完成\n请求URL{}\n请求参数\n{}\n返回参数\n{}",
url, formatLogValue(params), formatLogValue(response.getBody()));
return response.getBody();
} catch (Exception e) {
throw new RuntimeException("POST(form-urlencoded) 请求失败URL" + url + ",异常信息:" + e.getMessage(), e);
@@ -364,10 +370,31 @@ public class HttpUtils
requestEntity,
responseType
);
log.info("---------------------->POST(JSON) 请求成功URL{},响应结果:{}", url, response.getBody());
log.info("后端外部接口调用完成\n请求URL{}\n请求参数\n{}\n返回参数\n{}",
url, formatLogValue(requestBody), formatLogValue(response.getBody()));
return response.getBody();
} catch (Exception e) {
throw new RuntimeException("POST(JSON) 请求失败URL" + url + ",异常信息:" + e.getMessage(), e);
}
}
}
public static String formatLogValue(Object value)
{
if (value == null)
{
return "null";
}
if (value instanceof CharSequence)
{
return value.toString();
}
try
{
return JSON.toJSONString(value, JSONWriter.Feature.PrettyFormat);
}
catch (Exception e)
{
return String.valueOf(value);
}
}
}