OkHttp - Enable logs

I used Retrofit in order to make Http requests and Json parsing and I loved the way to turn on debug logs. Logs allow to see body requests, url... which is very usefull. As Retrofit use OkHttp, I'm wondering if OkHttp also have a way to enable logs for each requests made.

Answers


The interceptors feature is currently in review, but you can build your own version of okHttp with the feature by applying the code changes in the pull request.

You can implement the functionality you want with something like this

// Create an interceptor which catches requests and logs the info you want
RequestInterceptor logRequests= new RequestInterceptor() {
  public Request execute(Request request) {
    Log.i("REQUEST INFO", request.toString());
    return request; // return the request unaltered
  }
};

OkHttpClient client = new OkHttpClient();
List<RequestInterceptor> requestInterceptors = client.requestInterceptors();
requestInterceptros.add(logRequests);

A test is included within the pull request if you want to see more.

I'm going to have to warn you ahead of time about using this. There may be changes to the interceptor API since it has yet to be merged in. Don't use it with production code, but it's innocuous enough for personal testing.


Using an Interceptor, you can define the following class:

class LoggingInterceptor implements Interceptor {
  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Log.d("OkHttp", String.format("Sending request %s on %s%n%s",
        request.url(), chain.connection(), request.headers()));

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    Log.d("OkHttp", String.format("Received response for %s in %.1fms%n%s",
        response.request().url(), (t2 - t1) / 1e6d, response.headers()));

    return response;
  }
}

And add it:

OkHttpClient client = new OkHttpClient.Builder()
  .addInterceptor(new LoggingInterceptor())
  .build();

None yet. But there's an interceptors feature under development that should make it easy.


There's an official solution from Square (employee) now. You can try: https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor


you can enble logging and integerate with Timber to log only in debug.

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
      @Override
      public void log(String message) {
        Timber.tag("OkHttp: ");
        Timber.i(message);
      }
    }).setLevel(HttpLoggingInterceptor.Level.BODY);

   client = new OkHttpClient.Builder()
      .addInterceptor(httpLoggingInterceptor)
      .build();

For better ui and debugging of OkHttp Network calls you can use libraries like GANDER

Others features include :

  1. Apps using Gander will display a notification showing a summary of ongoing HTTP activity. Tapping on the notification launches the full Gander UI. Apps can optionally suppress the notification, and launch the Gander UI directly from within their own interface. HTTP interactions and their contents can be exported via a share intent.

  2. Search HTTP Activity and also request and response

  3. The main Gander activity is launched in its own task, allowing it to be displayed alongside the host app UI using Android 7.x multi-window support.
  4. Gander Provides following variants
    • Persistence : Saves logs to disk and TTL can be controlled
    • In Memory Database : Logs will be in memory as long as the app lifecycle.
    • No Op : This does nothing. So if users want Gander only in debug builds they can releaseCompile NoOp without dealing with variants, if(Build.DEBUG) ..etc

Need Your Help

Why does find -exec mv {} ./target/ + not work?

linux find cygwin exec

I want to know exactly what {} \; and {} \+ and | xargs ... do. Please clarify these with explanations.

allocating an object of abstract class type error

c++ abstract-class

Hi I'm getting the following error and am really unsure why.