Skip to content

Commit

Permalink
支持同一个请求的日志聚合在一起打印
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed May 31, 2022
1 parent 254d691 commit 5b19d84
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
15 changes: 13 additions & 2 deletions README.md
Expand Up @@ -48,7 +48,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.3.3</version>
<version>2.3.4</version>
</dependency>
```

Expand All @@ -59,7 +59,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.3.3</version>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down Expand Up @@ -432,6 +432,17 @@ retrofit:

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

#### 聚合日志打印

如果需要将同一个请求的日志聚合在一起打印,可配置`AggregateLoggingInterceptor`

```java
@Bean
public LoggingInterceptor loggingInterceptor(RetrofitProperties retrofitProperties){
return new AggregateLoggingInterceptor(retrofitProperties.getGlobalLog());
}
```

### 请求重试

组件支持支持全局重试和声明式重试。
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.3.3</version>
<version>2.3.4</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
@@ -0,0 +1,55 @@
package com.github.lianjiatech.retrofit.spring.boot.log;

import java.io.IOException;

import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
* 同一个请求的日志聚合在一起打印。 The logs of the same request are aggregated and printed together.
* @author 陈添明
* @since 2022/5/31 10:49 上午
*/
public class AggregateLoggingInterceptor extends LoggingInterceptor {

public AggregateLoggingInterceptor(GlobalLogProperty globalLogProperty) {
super(globalLogProperty);
}

@Override
public Response intercept(Chain chain) throws IOException {
Logging logging = findLogging(chain);
if (!needLog(logging)) {
return chain.proceed(chain.request());
}
LogLevel logLevel = logging == null ? globalLogProperty.getLogLevel() : logging.logLevel();
LogStrategy logStrategy = logging == null ? globalLogProperty.getLogStrategy() : logging.logStrategy();
BufferingLogger bufferingLogger = new BufferingLogger(matchLogger(logLevel));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(bufferingLogger)
.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
Response response = httpLoggingInterceptor.intercept(chain);
bufferingLogger.flush();
return response;
}

private static class BufferingLogger implements HttpLoggingInterceptor.Logger {

private StringBuilder buffer = new StringBuilder(System.lineSeparator());

private final HttpLoggingInterceptor.Logger delegate;

public BufferingLogger(HttpLoggingInterceptor.Logger delegate) {
this.delegate = delegate;
}

@Override
public void log(String message) {
buffer.append(message).append(System.lineSeparator());
}

public void flush() {
delegate.log(buffer.toString());
buffer = new StringBuilder(System.lineSeparator());
}
}
}
Expand Up @@ -8,7 +8,6 @@

import lombok.extern.slf4j.Slf4j;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Invocation;
Expand All @@ -28,19 +27,22 @@ public LoggingInterceptor(GlobalLogProperty globalLogProperty) {

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Method method = Objects.requireNonNull(request.tag(Invocation.class)).method();
Logging logging = AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(), Logging.class);
Logging logging = findLogging(chain);
if (!needLog(logging)) {
return chain.proceed(request);
return chain.proceed(chain.request());
}
LogLevel logLevel = logging == null ? globalLogProperty.getLogLevel() : logging.logLevel();
LogStrategy logStrategy = logging == null ? globalLogProperty.getLogStrategy() : logging.logStrategy();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(matchLogger(logLevel));
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(matchLogger(logLevel))
.setLevel(HttpLoggingInterceptor.Level.valueOf(logStrategy.name()));
return httpLoggingInterceptor.intercept(chain);
}

protected Logging findLogging(Chain chain) {
Method method = Objects.requireNonNull(chain.request().tag(Invocation.class)).method();
return AnnotationExtendUtils.findMergedAnnotation(method, method.getDeclaringClass(), Logging.class);
}

protected boolean needLog(Logging logging) {
if (globalLogProperty.isEnable()) {
if (logging == null) {
Expand Down
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.stream.IntStream;

import org.junit.After;
import org.junit.Assert;
Expand All @@ -19,6 +20,7 @@
import org.springframework.test.context.junit4.SpringRunner;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.lianjiatech.retrofit.spring.boot.test.entity.Person;
Expand Down Expand Up @@ -107,6 +109,40 @@ public void testGetPersonBody() throws Exception {
System.out.println(personBody);
}

@Test
public void testAggregateLoggingInterceptor() {
IntStream.range(1, 1000)
.parallel()
.forEach(i -> {
// mock
Person mockPerson = new Person().setId(1L)
.setName("test")
.setAge(10);
Result mockResult = new Result<>()
.setCode(0)
.setMsg("ok")
.setData(mockPerson);
MockResponse response = null;
try {
response = new MockResponse()
.setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8")
.addHeader("Cache-Control", "no-cache")
.setBody(objectMapper.writeValueAsString(mockResult));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
server.enqueue(response);

Person person = new Person();
person.setId(100L)
.setAge(10)
.setName("xx");
Result<Person> personBody = httpApi2.getPersonBody(person);
System.out.println(personBody);
});
}

@Test
public void testRetrofitConfigRef() throws IOException {

Expand Down
Expand Up @@ -3,6 +3,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;

import com.github.lianjiatech.retrofit.spring.boot.config.RetrofitProperties;
import com.github.lianjiatech.retrofit.spring.boot.log.AggregateLoggingInterceptor;
import com.github.lianjiatech.retrofit.spring.boot.log.LoggingInterceptor;

import lombok.extern.slf4j.Slf4j;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.jaxb.JaxbConverterFactory;
Expand Down Expand Up @@ -32,4 +36,10 @@ public JaxbConverterFactory jaxbConverterFactory() {
public InvalidRespErrorDecoder invalidRespErrorDecoder() {
return new InvalidRespErrorDecoder();
}

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean
public LoggingInterceptor loggingInterceptor(RetrofitProperties retrofitProperties) {
return new AggregateLoggingInterceptor(retrofitProperties.getGlobalLog());
}
}

0 comments on commit 5b19d84

Please sign in to comment.