forked from LianjiaTech/retrofit-spring-boot-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8db1e9
commit c29b776
Showing
6 changed files
with
124 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ain/java/com/github/lianjiatech/retrofit/spring/boot/log/AggregateLoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters