Skip to content

Commit 2a73403

Browse files
authored
Merge pull request #4 from razkl/add-option-to-set-log-level-on-request
add option to set log level on request
2 parents d5ed0ef + 13ecb75 commit 2a73403

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

src/main/java/com/taboola/rest/api/RestAPIClient.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.taboola.rest.api;
22

3+
import okhttp3.logging.HttpLoggingInterceptor;
4+
35
import java.util.ArrayList;
46
import java.util.Collection;
57
import java.util.List;
@@ -18,6 +20,7 @@
1820
import com.taboola.rest.api.internal.config.SerializationConfig;
1921
import com.taboola.rest.api.internal.config.UserAgentHeader;
2022
import com.taboola.rest.api.internal.serialization.SerializationMapperCreator;
23+
import com.taboola.rest.api.model.HttpLoggingLevel;
2124
import com.taboola.rest.api.model.RequestHeader;
2225
import com.taboola.rest.api.model.StringResponseFactory;
2326

@@ -72,6 +75,12 @@ public static class RestAPIClientBuilder {
7275
private ExceptionFactory exceptionFactory;
7376
private ObjectMapper objectMapper;
7477
private final StringResponseFactories stringResponseFactories = new StringResponseFactories();
78+
private HttpLoggingLevel loggingLevel;
79+
80+
public RestAPIClientBuilder setLoggingLevel(HttpLoggingLevel loggingLevel) {
81+
this.loggingLevel = loggingLevel;
82+
return this;
83+
}
7584

7685
public RestAPIClientBuilder setBaseUrl(String baseUrl) {
7786
this.baseUrl = baseUrl;
@@ -153,7 +162,7 @@ public RestAPIClient build() {
153162
String finalUserAgent = String.format("%s/%s/%s (%s)", userAgentPrefix, restAPIVersion, VERSION, userAgentSuffix);
154163
Collection<RequestHeader> headers = getAllHeaders(this.headers, finalUserAgent);
155164
CommunicationConfig config = new CommunicationConfig(baseUrl, connectionTimeoutMillis, readTimeoutMillis, writeTimeoutMillis, maxIdleConnections,
156-
keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories);
165+
keepAliveDurationMillis, headers, debug, exceptionFactory, objectMapper, stringResponseFactories, loggingLevel);
157166
return new RestAPIClient(new CommunicationFactory(config));
158167
}
159168

@@ -220,6 +229,10 @@ private void organizeState() {
220229
if (objectMapper == null) {
221230
objectMapper = SerializationMapperCreator.createObjectMapper(serializationConfig);
222231
}
232+
233+
if (loggingLevel == null) {
234+
loggingLevel = HttpLoggingLevel.BASIC;
235+
}
223236
}
224237
}
225238

src/main/java/com/taboola/rest/api/internal/CommunicationFactory.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
import com.taboola.rest.api.internal.config.CommunicationConfig;
66
import com.taboola.rest.api.internal.interceptors.CommunicationInterceptor;
77
import com.taboola.rest.api.internal.interceptors.HeadersInterceptor;
8+
import com.taboola.rest.api.model.HttpLoggingLevel;
89

910
import okhttp3.ConnectionPool;
1011
import okhttp3.OkHttpClient;
1112
import okhttp3.logging.HttpLoggingInterceptor;
1213
import retrofit2.Retrofit;
1314
import retrofit2.converter.jackson.JacksonConverterFactory;
1415

16+
import org.apache.logging.log4j.LogManager;
17+
import org.apache.logging.log4j.Logger;
18+
1519
/**
1620
* Created by vladi
1721
* Date: 10/4/2017
1822
* Time: 10:54 PM
1923
* By Taboola
2024
*/
2125
public final class CommunicationFactory {
22-
26+
private static final Logger logger = LogManager.getLogger(CommunicationFactory.class);
2327
private final Retrofit retrofit;
2428

2529
public CommunicationFactory(CommunicationConfig communicationConfig) {
@@ -32,12 +36,13 @@ private HttpLoggingInterceptor createLoggingInterceptor(CommunicationConfig conf
3236
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new CommunicationInterceptor());
3337
if (config.isDebug()) {
3438
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
39+
} else {
40+
loggingInterceptor.setLevel(getHttpLoggingInterceptorLevel(config.getLoggingLevel()));
41+
}
42+
if (loggingInterceptor.getLevel() == HttpLoggingInterceptor.Level.BODY || loggingInterceptor.getLevel() == HttpLoggingInterceptor.Level.HEADERS) {
3543
loggingInterceptor.redactHeader("Authorization");
3644
loggingInterceptor.redactHeader("Cookie");
37-
} else {
38-
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
3945
}
40-
4146
return loggingInterceptor;
4247
}
4348

@@ -61,6 +66,23 @@ private OkHttpClient createOkHttpClient(CommunicationConfig config) {
6166
.build();
6267
}
6368

69+
70+
public static HttpLoggingInterceptor.Level getHttpLoggingInterceptorLevel(HttpLoggingLevel httpLoggingLevel) {
71+
switch (httpLoggingLevel) {
72+
case NONE:
73+
return HttpLoggingInterceptor.Level.NONE;
74+
case BASIC:
75+
return HttpLoggingInterceptor.Level.BASIC;
76+
case HEADERS:
77+
return HttpLoggingInterceptor.Level.HEADERS;
78+
case BODY:
79+
return HttpLoggingInterceptor.Level.BODY;
80+
default:
81+
logger.error("Getting unknown HttpLoggingLevel [{}]", httpLoggingLevel.name());
82+
return HttpLoggingInterceptor.Level.BASIC;
83+
}
84+
}
85+
6486
public <E> E createRetrofitEndpoint(Class<E> clazz) {
6587
return retrofit.create(clazz);
6688
}

src/main/java/com/taboola/rest/api/internal/config/CommunicationConfig.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import com.taboola.rest.api.exceptions.factories.ExceptionFactory;
88
import com.taboola.rest.api.internal.StringResponseFactories;
9+
import com.taboola.rest.api.model.HttpLoggingLevel;
910
import com.taboola.rest.api.model.RequestHeader;
1011

1112
/**
@@ -27,11 +28,12 @@ public class CommunicationConfig {
2728
private final ExceptionFactory exceptionFactory;
2829
private final ObjectMapper objectMapper;
2930
private final StringResponseFactories stringResponseFactories;
31+
private final HttpLoggingLevel loggingLevel;
3032

3133
public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long readTimeoutMillis,
3234
Long writeTimeoutMillis, Integer maxIdleConnections, Long keepAliveDurationMillis,
3335
Collection<RequestHeader> headers, boolean debug, ExceptionFactory exceptionFactory,
34-
ObjectMapper objectMapper, StringResponseFactories stringResponseFactories) {
36+
ObjectMapper objectMapper, StringResponseFactories stringResponseFactories, HttpLoggingLevel loggingLevel) {
3537
this.baseUrl = baseUrl;
3638
this.connectionTimeoutMillis = connectionTimeoutMillis;
3739
this.readTimeoutMillis = readTimeoutMillis;
@@ -43,6 +45,7 @@ public CommunicationConfig(String baseUrl, Long connectionTimeoutMillis, Long re
4345
this.exceptionFactory = exceptionFactory;
4446
this.objectMapper = objectMapper;
4547
this.stringResponseFactories = stringResponseFactories;
48+
this.loggingLevel = loggingLevel;
4649
}
4750

4851
public String getBaseUrl() {
@@ -89,6 +92,10 @@ public StringResponseFactories getStringResponseFactories() {
8992
return stringResponseFactories;
9093
}
9194

95+
public HttpLoggingLevel getLoggingLevel() {
96+
return loggingLevel;
97+
}
98+
9299
@Override
93100
public String toString() {
94101
return "CommunicationConfig{" +
@@ -103,6 +110,7 @@ public String toString() {
103110
", exceptionFactory=" + exceptionFactory +
104111
", objectMapper=" + objectMapper +
105112
", stringResponseFactories=" + stringResponseFactories +
113+
", loggingLevel=" + loggingLevel +
106114
'}';
107115
}
108116
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.taboola.rest.api.model;
2+
3+
public enum HttpLoggingLevel {
4+
5+
NONE,
6+
BASIC,
7+
HEADERS,
8+
BODY;
9+
}

src/test/java/com/taboola/rest/api/internal/CommunicationFactoryTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import com.taboola.rest.api.exceptions.factories.DefaultExceptionFactory;
1212
import com.taboola.rest.api.internal.config.CommunicationConfig;
1313
import com.taboola.rest.api.internal.config.UserAgentHeader;
14+
import com.taboola.rest.api.model.HttpLoggingLevel;
1415

16+
import okhttp3.logging.HttpLoggingInterceptor;
1517
import retrofit2.http.GET;
1618
import retrofit2.http.Header;
1719
import retrofit2.http.Headers;
@@ -37,7 +39,7 @@ public void beforeTest() {
3739
CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost",
3840
1L, 1L, 1L, 1, 60L,
3941
Collections.singleton(new UserAgentHeader("Dummy-Agent")),true, new DefaultExceptionFactory(),
40-
new ObjectMapper(), new StringResponseFactories());
42+
new ObjectMapper(), new StringResponseFactories(), HttpLoggingLevel.BASIC);
4143
testInstance = new CommunicationFactory(communicationConfig);
4244
}
4345

0 commit comments

Comments
 (0)