Skip to content

Commit 5b19d84

Browse files
支持同一个请求的日志聚合在一起打印
1 parent 254d691 commit 5b19d84

File tree

6 files changed

+124
-10
lines changed

6 files changed

+124
-10
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
4848
<dependency>
4949
<groupId>com.github.lianjiatech</groupId>
5050
<artifactId>retrofit-spring-boot-starter</artifactId>
51-
<version>2.3.3</version>
51+
<version>2.3.4</version>
5252
</dependency>
5353
```
5454

@@ -59,7 +59,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
5959
<dependency>
6060
<groupId>com.github.lianjiatech</groupId>
6161
<artifactId>retrofit-spring-boot-starter</artifactId>
62-
<version>2.3.3</version>
62+
<version>2.3.4</version>
6363
</dependency>
6464
<dependency>
6565
<groupId>com.squareup.okhttp3</groupId>
@@ -432,6 +432,17 @@ retrofit:
432432

433433
如果需要修改日志打印行为,继承`LoggingInterceptor`,并将其配置成Spring bean即可!
434434

435+
#### 聚合日志打印
436+
437+
如果需要将同一个请求的日志聚合在一起打印,可配置`AggregateLoggingInterceptor`。
438+
439+
```java
440+
@Bean
441+
public LoggingInterceptor loggingInterceptor(RetrofitProperties retrofitProperties){
442+
return new AggregateLoggingInterceptor(retrofitProperties.getGlobalLog());
443+
}
444+
```
445+
435446
### 请求重试
436447

437448
组件支持支持全局重试和声明式重试。

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.lianjiatech</groupId>
88
<artifactId>retrofit-spring-boot-starter</artifactId>
9-
<version>2.3.3</version>
9+
<version>2.3.4</version>
1010

1111
<name>retrofit-spring-boot-starter</name>
1212
<description>retrofit-spring-boot-starter</description>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.github.lianjiatech.retrofit.spring.boot.log;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Response;
6+
import okhttp3.logging.HttpLoggingInterceptor;
7+
8+
/**
9+
* 同一个请求的日志聚合在一起打印。 The logs of the same request are aggregated and printed together.
10+
* @author 陈添明
11+
* @since 2022/5/31 10:49 上午
12+
*/
13+
public class AggregateLoggingInterceptor extends LoggingInterceptor {
14+
15+
public AggregateLoggingInterceptor(GlobalLogProperty globalLogProperty) {
16+
super(globalLogProperty);
17+
}
18+
19+
@Override
20+
public Response intercept(Chain chain) throws IOException {
21+
Logging logging = findLogging(chain);
22+
if (!needLog(logging)) {
23+
return chain.proceed(chain.request());
24+
}
25+
LogLevel logLevel = logging == null ? globalLogProperty.getLogLevel() : logging.logLevel();
26+
LogStrategy logStrategy = logging == null ? globalLogProperty.getLogStrategy() : logging.logStrategy();
27+
BufferingLogger bufferingLogger = new BufferingLogger(matchLogger(logLevel));
28+
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(bufferingLogger)
29+
.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
30+
Response response = httpLoggingInterceptor.intercept(chain);
31+
bufferingLogger.flush();
32+
return response;
33+
}
34+
35+
private static class BufferingLogger implements HttpLoggingInterceptor.Logger {
36+
37+
private StringBuilder buffer = new StringBuilder(System.lineSeparator());
38+
39+
private final HttpLoggingInterceptor.Logger delegate;
40+
41+
public BufferingLogger(HttpLoggingInterceptor.Logger delegate) {
42+
this.delegate = delegate;
43+
}
44+
45+
@Override
46+
public void log(String message) {
47+
buffer.append(message).append(System.lineSeparator());
48+
}
49+
50+
public void flush() {
51+
delegate.log(buffer.toString());
52+
buffer = new StringBuilder(System.lineSeparator());
53+
}
54+
}
55+
}

src/main/java/com/github/lianjiatech/retrofit/spring/boot/log/LoggingInterceptor.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import lombok.extern.slf4j.Slf4j;
1010
import okhttp3.Interceptor;
11-
import okhttp3.Request;
1211
import okhttp3.Response;
1312
import okhttp3.logging.HttpLoggingInterceptor;
1413
import retrofit2.Invocation;
@@ -28,19 +27,22 @@ public LoggingInterceptor(GlobalLogProperty globalLogProperty) {
2827

2928
@Override
3029
public Response intercept(Chain chain) throws IOException {
31-
Request request = chain.request();
32-
Method method = Objects.requireNonNull(request.tag(Invocation.class)).method();
33-
Logging logging = AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(), Logging.class);
30+
Logging logging = findLogging(chain);
3431
if (!needLog(logging)) {
35-
return chain.proceed(request);
32+
return chain.proceed(chain.request());
3633
}
3734
LogLevel logLevel = logging == null ? globalLogProperty.getLogLevel() : logging.logLevel();
3835
LogStrategy logStrategy = logging == null ? globalLogProperty.getLogStrategy() : logging.logStrategy();
39-
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(matchLogger(logLevel));
40-
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
36+
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(matchLogger(logLevel))
37+
.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
4138
return httpLoggingInterceptor.intercept(chain);
4239
}
4340

41+
protected Logging findLogging(Chain chain) {
42+
Method method = Objects.requireNonNull(chain.request().tag(Invocation.class)).method();
43+
return AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(), Logging.class);
44+
}
45+
4446
protected boolean needLog(Logging logging) {
4547
if (globalLogProperty.isEnable()) {
4648
if (logging == null) {

src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitStarterTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.concurrent.CompletableFuture;
99
import java.util.concurrent.CountDownLatch;
1010
import java.util.concurrent.ExecutionException;
11+
import java.util.stream.IntStream;
1112

1213
import org.junit.After;
1314
import org.junit.Assert;
@@ -19,6 +20,7 @@
1920
import org.springframework.test.context.junit4.SpringRunner;
2021

2122
import com.fasterxml.jackson.annotation.JsonInclude;
23+
import com.fasterxml.jackson.core.JsonProcessingException;
2224
import com.fasterxml.jackson.databind.DeserializationFeature;
2325
import com.fasterxml.jackson.databind.ObjectMapper;
2426
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
@@ -107,6 +109,40 @@ public void testGetPersonBody() throws Exception {
107109
System.out.println(personBody);
108110
}
109111

112+
@Test
113+
public void testAggregateLoggingInterceptor() {
114+
IntStream.range(1, 1000)
115+
.parallel()
116+
.forEach(i -> {
117+
// mock
118+
Person mockPerson = new Person().setId(1L)
119+
.setName("test")
120+
.setAge(10);
121+
Result mockResult = new Result<>()
122+
.setCode(0)
123+
.setMsg("ok")
124+
.setData(mockPerson);
125+
MockResponse response = null;
126+
try {
127+
response = new MockResponse()
128+
.setResponseCode(200)
129+
.addHeader("Content-Type", "application/json; charset=utf-8")
130+
.addHeader("Cache-Control", "no-cache")
131+
.setBody(objectMapper.writeValueAsString(mockResult));
132+
} catch (JsonProcessingException e) {
133+
e.printStackTrace();
134+
}
135+
server.enqueue(response);
136+
137+
Person person = new Person();
138+
person.setId(100L)
139+
.setAge(10)
140+
.setName("xx");
141+
Result<Person> personBody = httpApi2.getPersonBody(person);
142+
System.out.println(personBody);
143+
});
144+
}
145+
110146
@Test
111147
public void testRetrofitConfigRef() throws IOException {
112148

src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/RetrofitTestApplication.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.context.annotation.Bean;
55

6+
import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties;
7+
import com.github.lianjiatech.retrofit.spring.boot.log.AggregateLoggingInterceptor;
8+
import com.github.lianjiatech.retrofit.spring.boot.log.LoggingInterceptor;
9+
610
import lombok.extern.slf4j.Slf4j;
711
import retrofit2.converter.gson.GsonConverterFactory;
812
import retrofit2.converter.jaxb.JaxbConverterFactory;
@@ -32,4 +36,10 @@ public JaxbConverterFactory jaxbConverterFactory() {
3236
public InvalidRespErrorDecoder invalidRespErrorDecoder() {
3337
return new InvalidRespErrorDecoder();
3438
}
39+
40+
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
41+
@Bean
42+
public LoggingInterceptor loggingInterceptor(RetrofitProperties retrofitProperties) {
43+
return new AggregateLoggingInterceptor(retrofitProperties.getGlobalLog());
44+
}
3545
}

0 commit comments

Comments
 (0)