Skip to content

Commit

Permalink
Allow user pick their own logger with custom config instead (#1455)
Browse files Browse the repository at this point in the history
An implementation for this discussion
#1415

---------

Co-authored-by: Motoi Okuzono <[email protected]>
  • Loading branch information
trinhnx and mokuzon authored Dec 19, 2024
1 parent d07ee01 commit a24b0e2
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public ApiClientBuilder(URI apiEndPoint, Class<T> clientClass, ExceptionBuilder

private Integer maxRequestsPerHost = 5;

private boolean usingDefaultLogger = true;

/**
* API Endpoint.
*/
Expand Down Expand Up @@ -158,7 +160,16 @@ public ApiClientBuilder<T> maxRequestsPerHost(Integer maxRequestsPerHost) {
return this;
}

private static Interceptor buildLoggingInterceptor() {
/**
* Use default logger (see {@link #buildLoggingInterceptor()}) , default is true
* @param usingDefaultLogger
*/
public ApiClientBuilder<T> usingDefaultLogger(boolean usingDefaultLogger) {
this.usingDefaultLogger = usingDefaultLogger;
return this;
}

public static Interceptor buildLoggingInterceptor() {
final Logger slf4jLogger = LoggerFactory.getLogger("com.linecorp.bot.client.wire");

return new HttpLoggingInterceptor(slf4jLogger::info)
Expand Down Expand Up @@ -191,7 +202,10 @@ Dispatcher createDispatcher() {
public T build() {
OkHttpClient.Builder okHttpClientBuilder = createBuilder();
additionalInterceptors.forEach(okHttpClientBuilder::addInterceptor);
okHttpClientBuilder.addInterceptor(buildLoggingInterceptor());
// Either adding explicitly HttpInterceptor#loggingInterceptor() or write your own
if (usingDefaultLogger) {
okHttpClientBuilder.addInterceptor(buildLoggingInterceptor());
}

// Set timeout.
okHttpClientBuilder
Expand Down

0 comments on commit a24b0e2

Please sign in to comment.