feat: 日志优化

This commit is contained in:
wangyu 2023-12-28 10:21:11 +08:00
parent 18bfda32ff
commit 329e258846
8 changed files with 91 additions and 7 deletions

View File

@ -4,7 +4,7 @@ Rest Proxy组件是本人在长期的系统对接工作中提炼出的一个可
具体是这样的:**写一个interface定义几个method然后加上注解完成**在业务里直接调用method即可不需要写任何实现类
## REST请求代理基于Http Interface
本组件基于SpringBoot2.7.7构建支持完整的AutoConfiguraiton。仅需要极少量配置就可以完成配置。当前最新版本为`1.1.5`新增了标准REST注解。
本组件基于SpringBoot2.7.7构建支持完整的AutoConfiguraiton。仅需要极少量配置就可以完成配置。当前最新版本为`1.1.6`新增了标准REST注解。
### 特性列表
- 无感知自动注入只需interface + 注解即可轻松完成对接
@ -29,7 +29,7 @@ Rest Proxy组件是本人在长期的系统对接工作中提炼出的一个可
<dependency>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy-core</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</dependency>
```

View File

@ -13,7 +13,7 @@
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
@ -22,7 +22,7 @@
<java.version>1.8</java.version>
<commons-collection.version>4.4</commons-collection.version>
<commons.lang.version>2.6</commons.lang.version>
<sdk.version>1.1.5</sdk.version>
<sdk.version>1.1.6</sdk.version>
</properties>
<packaging>pom</packaging>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<groupId>group.flyfish</groupId>
<artifactId>rest-proxy</artifactId>
<version>1.1.5</version>
<version>1.1.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -8,6 +8,7 @@ import group.flyfish.rest.core.factory.HttpClientFactoryBean;
import group.flyfish.rest.core.factory.HttpClientProvider;
import group.flyfish.rest.enums.ResponseType;
import group.flyfish.rest.utils.JacksonUtil;
import group.flyfish.rest.utils.RestLogUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
@ -272,8 +273,8 @@ final class DefaultRestClient extends RestErrorHandler implements RestClient {
* @return 结果
*/
private <T> T innerExecute() throws IOException {
log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI());
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
RestLogUtils.log(response);
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {

View File

@ -17,6 +17,7 @@ import group.flyfish.rest.registry.proxy.support.UrlCompiler;
import group.flyfish.rest.registry.wrapper.DefaultRestResultMapping;
import group.flyfish.rest.utils.DataUtils;
import group.flyfish.rest.utils.JacksonUtil;
import group.flyfish.rest.utils.RestLogUtils;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -161,8 +162,12 @@ public class RestProxyInvoker implements InvocationHandler, PropertiesConfigurab
client.setClient(registry.getProvider());
// 是否对结果进行映射
boolean map = null != mapping && !method.isBare();
// 打印请求
RestLogUtils.log(builder);
// 执行请求
Object result = execute(client, method, map);
// 打印结果
RestLogUtils.log(result);
// 结果映射
return map ? mapping.map(result) : result;
}

View File

@ -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(";"));
}
}

View File

@ -7,3 +7,8 @@ rest:
dsep: http://220.194.160.6:8091/jeecg-boot/api
bdmp: http://localhost:8081
damp: http://60.221.255.208:18092/api
debug: true
logging:
level:
group.flyfish.rest: debug