feat: 日志优化
This commit is contained in:
parent
18bfda32ff
commit
329e258846
@ -4,7 +4,7 @@ Rest Proxy组件是本人在长期的系统对接工作中提炼出的一个可
|
|||||||
具体是这样的:**写一个interface,定义几个method,然后加上注解,完成!**,在业务里直接调用method即可,不需要写任何实现类!
|
具体是这样的:**写一个interface,定义几个method,然后加上注解,完成!**,在业务里直接调用method即可,不需要写任何实现类!
|
||||||
|
|
||||||
## REST请求代理(基于Http Interface)
|
## REST请求代理(基于Http Interface)
|
||||||
本组件基于SpringBoot2.7.7构建,支持完整的AutoConfiguraiton。仅需要极少量配置就可以完成配置。当前最新版本为`1.1.5`,新增了标准REST注解。
|
本组件基于SpringBoot2.7.7构建,支持完整的AutoConfiguraiton。仅需要极少量配置就可以完成配置。当前最新版本为`1.1.6`,新增了标准REST注解。
|
||||||
|
|
||||||
### 特性列表
|
### 特性列表
|
||||||
- 无感知自动注入,只需interface + 注解即可轻松完成对接
|
- 无感知自动注入,只需interface + 注解即可轻松完成对接
|
||||||
@ -29,7 +29,7 @@ Rest Proxy组件是本人在长期的系统对接工作中提炼出的一个可
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy-core</artifactId>
|
<artifactId>rest-proxy-core</artifactId>
|
||||||
<version>1.1.5</version>
|
<version>1.1.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
4
pom.xml
4
pom.xml
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.1.5</version>
|
<version>1.1.6</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>8</maven.compiler.source>
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
@ -22,7 +22,7 @@
|
|||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<commons-collection.version>4.4</commons-collection.version>
|
<commons-collection.version>4.4</commons-collection.version>
|
||||||
<commons.lang.version>2.6</commons.lang.version>
|
<commons.lang.version>2.6</commons.lang.version>
|
||||||
<sdk.version>1.1.5</sdk.version>
|
<sdk.version>1.1.6</sdk.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.1.5</version>
|
<version>1.1.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.1.5</version>
|
<version>1.1.6</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import group.flyfish.rest.core.factory.HttpClientFactoryBean;
|
|||||||
import group.flyfish.rest.core.factory.HttpClientProvider;
|
import group.flyfish.rest.core.factory.HttpClientProvider;
|
||||||
import group.flyfish.rest.enums.ResponseType;
|
import group.flyfish.rest.enums.ResponseType;
|
||||||
import group.flyfish.rest.utils.JacksonUtil;
|
import group.flyfish.rest.utils.JacksonUtil;
|
||||||
|
import group.flyfish.rest.utils.RestLogUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.StatusLine;
|
import org.apache.http.StatusLine;
|
||||||
@ -272,8 +273,8 @@ final class DefaultRestClient extends RestErrorHandler implements RestClient {
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
private <T> T innerExecute() throws IOException {
|
private <T> T innerExecute() throws IOException {
|
||||||
log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI());
|
|
||||||
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
|
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
|
||||||
|
RestLogUtils.log(response);
|
||||||
StatusLine statusLine = response.getStatusLine();
|
StatusLine statusLine = response.getStatusLine();
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {
|
if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {
|
||||||
|
@ -17,6 +17,7 @@ import group.flyfish.rest.registry.proxy.support.UrlCompiler;
|
|||||||
import group.flyfish.rest.registry.wrapper.DefaultRestResultMapping;
|
import group.flyfish.rest.registry.wrapper.DefaultRestResultMapping;
|
||||||
import group.flyfish.rest.utils.DataUtils;
|
import group.flyfish.rest.utils.DataUtils;
|
||||||
import group.flyfish.rest.utils.JacksonUtil;
|
import group.flyfish.rest.utils.JacksonUtil;
|
||||||
|
import group.flyfish.rest.utils.RestLogUtils;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -161,8 +162,12 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigurab
|
|||||||
client.setClient(registry.getProvider());
|
client.setClient(registry.getProvider());
|
||||||
// 是否对结果进行映射
|
// 是否对结果进行映射
|
||||||
boolean map = null != mapping && !method.isBare();
|
boolean map = null != mapping && !method.isBare();
|
||||||
|
// 打印请求
|
||||||
|
RestLogUtils.log(builder);
|
||||||
// 执行请求
|
// 执行请求
|
||||||
Object result = execute(client, method, map);
|
Object result = execute(client, method, map);
|
||||||
|
// 打印结果
|
||||||
|
RestLogUtils.log(result);
|
||||||
// 结果映射
|
// 结果映射
|
||||||
return map ? mapping.map(result) : result;
|
return map ? mapping.map(result) : result;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
package group.flyfish.rest.utils;
|
||||||
|
|
||||||
|
import group.flyfish.rest.core.client.RestClientBuilder;
|
||||||
|
import lombok.AccessLevel;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.http.HttpMessage;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志打印相关
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
@Slf4j
|
||||||
|
public final class RestLogUtils {
|
||||||
|
|
||||||
|
private static final String LOG_PREFIX = "【Rest Invoke】";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印请求日志
|
||||||
|
*
|
||||||
|
* @param builder 请求信息
|
||||||
|
*/
|
||||||
|
public static void log(RestClientBuilder builder) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("{} {} {}\nRequest Headers: {}\nParameters:{}\nBody:{}", LOG_PREFIX, builder.getMethod(),
|
||||||
|
builder.getUrl(), resolveMap(builder.getHeaders()), resolveMap(builder.getParams()), builder.getBody());
|
||||||
|
} else {
|
||||||
|
log.info("{} {} {}", LOG_PREFIX, builder.getMethod(), builder.getUrl());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印响应信息
|
||||||
|
*
|
||||||
|
* @param response 响应信息
|
||||||
|
*/
|
||||||
|
public static void log(CloseableHttpResponse response) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("{} Response Status: {}\nResponse Headers: {}\n", LOG_PREFIX,
|
||||||
|
response.getStatusLine().getStatusCode(), resolveHeaders(response));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印结果体
|
||||||
|
*
|
||||||
|
* @param result 结果
|
||||||
|
*/
|
||||||
|
public static void log(Object result) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("{} Response Body:{}", LOG_PREFIX, JacksonUtil.toJson(result).orElse("no body"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String resolveHeaders(HttpMessage message) {
|
||||||
|
return Arrays.stream(message.getAllHeaders())
|
||||||
|
.map(header -> String.join("=", header.getName(), header.getValue()))
|
||||||
|
.collect(Collectors.joining(";"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String resolveMap(Map<String, ?> headers) {
|
||||||
|
return headers.entrySet().stream()
|
||||||
|
.map(header -> String.join("=", header.getKey(), String.valueOf(header.getValue())))
|
||||||
|
.collect(Collectors.joining(";"));
|
||||||
|
}
|
||||||
|
}
|
@ -7,3 +7,8 @@ rest:
|
|||||||
dsep: http://220.194.160.6:8091/jeecg-boot/api
|
dsep: http://220.194.160.6:8091/jeecg-boot/api
|
||||||
bdmp: http://localhost:8081
|
bdmp: http://localhost:8081
|
||||||
damp: http://60.221.255.208:18092/api
|
damp: http://60.221.255.208:18092/api
|
||||||
|
|
||||||
|
debug: true
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
group.flyfish.rest: debug
|
||||||
|
Loading…
Reference in New Issue
Block a user