Skip to content

Commit 3012da2

Browse files
committed
Allow user pick their own logger with custom config instead
1 parent 763f1bc commit 3012da2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/ApiClientBuilder.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public ApiClientBuilder(URI apiEndPoint, Class<T> clientClass, ExceptionBuilder
9797

9898
private Integer maxRequestsPerHost = 5;
9999

100+
private boolean usingDefaultLogger = true; // Just kept the same behavior as the original code
101+
100102
/**
101103
* API Endpoint.
102104
*/
@@ -158,7 +160,16 @@ public ApiClientBuilder<T> maxRequestsPerHost(Integer maxRequestsPerHost) {
158160
return this;
159161
}
160162

161-
private static Interceptor buildLoggingInterceptor() {
163+
/**
164+
* Use default logger (see {@link #buildLoggingInterceptor()}) , default is true
165+
* @param usingDefaultLogger
166+
*/
167+
public ApiClientBuilder<T> usingDefaultLogger(boolean usingDefaultLogger) {
168+
this.usingDefaultLogger = usingDefaultLogger;
169+
return this;
170+
}
171+
172+
public static Interceptor buildLoggingInterceptor() {
162173
final Logger slf4jLogger = LoggerFactory.getLogger("com.linecorp.bot.client.wire");
163174

164175
return new HttpLoggingInterceptor(slf4jLogger::info)
@@ -191,7 +202,10 @@ Dispatcher createDispatcher() {
191202
public T build() {
192203
OkHttpClient.Builder okHttpClientBuilder = createBuilder();
193204
additionalInterceptors.forEach(okHttpClientBuilder::addInterceptor);
194-
okHttpClientBuilder.addInterceptor(buildLoggingInterceptor());
205+
// Either adding explicitly HttpInterceptor#loggingInterceptor() or write your own
206+
if (usingDefaultLogger) {
207+
okHttpClientBuilder.addInterceptor(buildLoggingInterceptor());
208+
}
195209

196210
// Set timeout.
197211
okHttpClientBuilder

clients/line-bot-client-base/src/main/java/com/linecorp/bot/client/base/http/HttpInterceptor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818

1919
import java.io.IOException;
2020

21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import okhttp3.Interceptor;
25+
import okhttp3.logging.HttpLoggingInterceptor;
26+
2127
public interface HttpInterceptor {
2228
HttpResponse intercept(HttpChain httpChain) throws IOException;
29+
30+
static Interceptor loggingInterceptor() {
31+
final Logger slf4jLogger = LoggerFactory.getLogger("com.linecorp.bot.client.wire");
32+
return new HttpLoggingInterceptor(slf4jLogger::info)
33+
.setLevel(HttpLoggingInterceptor.Level.BODY);
34+
}
2335
}

0 commit comments

Comments
 (0)