Reputation: 248
trying to use HttpLoggingInterceptor with Retrofit2, but have no result, any requests/responses doesn't show any logs.
There are libraries' versions i use:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
There is code where i'm generating api instance:
public class ApiGenerator {
private static final String BASE_URL = "http://api.openweathermap.org/data/2.5/";
private static final String API_KEY = "de4d4a35830d98e785a1cbb06725457f";
private ApiGenerator() {}
public static WeatherApi createWeatherApi() {
return createRetrofit().create(WeatherApi.class);
}
private static Retrofit createRetrofit() {
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(createClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
private static OkHttpClient createClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder().addInterceptor(chain -> {
Request request = chain.request();
HttpUrl url = request.url().newBuilder()
.addQueryParameter("APPID", API_KEY)
.build();
request.newBuilder().url(url).build();
return chain.proceed(request);
})
.addInterceptor(logging)
.build();
}
}
Upvotes: 3
Views: 2862
Reputation: 9086
A Kotlin version of Bill's answer
val log: Logger = LoggerFactory.getLogger("HttpLogging")
val okHttpLoggingInterceptor = HttpLoggingInterceptor { log.debug(it) }.setLevel(BODY)
Upvotes: -1
Reputation: 582
Your example is using a default logger which you may not have configured. A better solution would be to pass in a custom logger using your logging framework of choice. Example using SLF4J with Logback.
private static final Logger log = LoggerFactory.getLogger(HttpClient.class);
private static final HttpLoggingInterceptor loggingInterceptor =
new HttpLoggingInterceptor((msg) -> {
log.debug(msg);
});
static {
loggingInterceptor.setLevel(Level.BODY);
}
public static HttpLoggingInterceptor getLoggingInterceptor() {
return loggingInterceptor;
}
Upvotes: 6