Skip to content

Instantly share code, notes, and snippets.

@electrum
Created May 13, 2020 19:22
Show Gist options
  • Save electrum/8b94d2cd86c83170b9a5bfcbe103d71f to your computer and use it in GitHub Desktop.
Save electrum/8b94d2cd86c83170b9a5bfcbe103d71f to your computer and use it in GitHub Desktop.
OkHttp LoggingInterceptor
package io.prestosql.client;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
import static io.airlift.units.Duration.nanosSince;
import static java.lang.String.format;
public class LoggingInterceptor
implements Interceptor
{
@Override
public Response intercept(Interceptor.Chain chain)
throws IOException
{
Request request = chain.request();
System.err.println(format("Sending request %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
long start = System.nanoTime();
Response response = chain.proceed(request);
System.err.println(format("Received response for %s in %s%n%s",
response.request().url(), nanosSince(start), response.headers()));
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment