Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpLoggingInterceptor not logging with retrofit 2

Im trying to log all the requests (with a network interceptor) using refrofit2, kotlin and logging-interceptor:

  • retrofit: "2.0.2"
  • okhttp3 : "3.2.0"
  • com.squareup.okhttp3:logging-interceptor 3.2.0

like:

val interceptor = HttpLoggingInterceptor()
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    val okHttpClient = OkHttpClient.Builder()
        .addNetworkInterceptor(interceptor) // same for .addInterceptor(...)
        .connectTimeout(30, TimeUnit.SECONDS) //Backend is really slow
        .writeTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .build()

    sRestAdapter = Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl(if (host.endsWith("/")) host else "$host/")
        .addConverterFactory(GsonConverterFactory.create(gson()))
        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
        .build()

It just print:

D/OkHttp: --> GET url...
D/OkHttp: --> END GET

What is happening?

--------------- EDIT --------

Errors doing requests on Main Thread are not showing by the logger, so be careful.

like image 850
Daniel Gomez Rico Avatar asked May 08 '16 21:05

Daniel Gomez Rico


People also ask

What is HttpLoggingInterceptor level body?

public static final HttpLoggingInterceptor.Level BODY. Logs request and response lines and their respective headers and bodies (if present). Example: --> POST /greeting http/1.1 Host: example.com Content-Type: plain/text Content-Length: 3 Hi?

What is HttpLoggingInterceptor?

With a HttpLoggingInterceptor, OkHttp will automatically log incoming and outgoing HTTP requests and responses to Logcat, where we can then see information like the type of request, the fully resolved URL, the content-type, the different HTTP headers, and the payload of the body itself, which contains the actual JSON, ...


2 Answers

Instead of

val okHttpClient = OkHttpClient.Builder()
    .addNetworkInterceptor(interceptor)
    ...

you should have something like:

val okHttpClient = OkHttpClient.Builder()
    .addInterceptor(interceptor)
    ...

as the addNetworkInterceptor() plays with interceptors that observe a single network request and response, while addInterceptor() adds interceptor that observes the full span of each call: from the connection is established (if any) until after the response source is selected (either the origin server, cache, or both).

EDIT

Errors doing requests on Main Thread are not showing by the logger, so be careful

Doing networking on main thread is not an "ordinary" error. It will result in your app being killed by the system with NetworkOnMainThreadException and this will happen before interceptor would be given any chance to run.

like image 91
Marcin Orlowski Avatar answered Sep 20 '22 17:09

Marcin Orlowski


private val interceptor = run {
    val httpLoggingInterceptor = HttpLoggingInterceptor()
    httpLoggingInterceptor.apply {
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
    }
}


private val okHttpClient = OkHttpClient.Builder()
    .addNetworkInterceptor(interceptor) // same for .addInterceptor(...)
    .connectTimeout(30, TimeUnit.SECONDS) //Backend is really slow
    .writeTimeout(30, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build()
like image 21
Sana Ebadi Avatar answered Sep 20 '22 17:09

Sana Ebadi