I am using Retrofit and I want to log my response and other things, I am using this https://futurestud.io/tutorials/retrofit-2-log-requests-and-responses type but I am facing Cant resolve setLevel error
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BODY); // i am getting error on this
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging); //this is also getting error
I am using this compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' and Retrofit compile 'com.squareup.retrofit2:converter-gson:2.1.0' dependency.
Logging In Retrofit 2HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); // set your desired log level logging. setLevel(Level. BODY); OkHttpClient. Builder httpClient = new OkHttpClient.
Class HttpLoggingInterceptorAn OkHttp interceptor which logs request and response information.
Add this dependency to your app's gradle:
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
And use it like below:
Build your OkHttpClient
like below:
final OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClientBuilder.addInterceptor(interceptor);
okHttpClientBuilder.connectTimeout(RestConstants.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
return okHttpClientBuilder.build();
And set it to your Retrofit
as Client.
I recommend you to check if your app is Debuggable
and than set your log interceptor. So in production you'll not log api results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With