Basic logging with redacted sensitive headers (#3159)

This commit is contained in:
Vivek Maskara 2019-10-05 17:06:35 +05:30 committed by Josephine Lim
parent 2e0281eaee
commit 13d847ea77
2 changed files with 12 additions and 1 deletions

View file

@ -53,6 +53,7 @@ dependencies {
api('com.github.tony19:logback-android-classic:1.1.1-6') { api('com.github.tony19:logback-android-classic:1.1.1-6') {
exclude group: 'com.google.android', module: 'android' exclude group: 'com.google.android', module: 'android'
} }
implementation "com.squareup.okhttp3:logging-interceptor:4.2.0"
// Dependency injector // Dependency injector
implementation "com.google.dagger:dagger:$DAGGER_VERSION" implementation "com.google.dagger:dagger:$DAGGER_VERSION"

View file

@ -31,12 +31,22 @@ public final class OkHttpConnectionFactory {
return new OkHttpClient.Builder() return new OkHttpClient.Builder()
.cookieJar(SharedPreferenceCookieManager.getInstance()) .cookieJar(SharedPreferenceCookieManager.getInstance())
.cache(NET_CACHE) .cache(NET_CACHE)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .addInterceptor(getLoggingInterceptor())
.addInterceptor(new UnsuccessfulResponseInterceptor()) .addInterceptor(new UnsuccessfulResponseInterceptor())
.addInterceptor(new CommonHeaderRequestInterceptor()) .addInterceptor(new CommonHeaderRequestInterceptor())
.build(); .build();
} }
private static HttpLoggingInterceptor getLoggingInterceptor() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpLoggingInterceptor.redactHeader("Authorization");
httpLoggingInterceptor.redactHeader("Cookie");
return httpLoggingInterceptor;
}
private static class CommonHeaderRequestInterceptor implements Interceptor { private static class CommonHeaderRequestInterceptor implements Interceptor {
@Override @NonNull public Response intercept(@NonNull Chain chain) throws IOException { @Override @NonNull public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request().newBuilder() Request request = chain.request().newBuilder()