Skip to content

Instantly share code, notes, and snippets.

@Avinash-Bhat
Last active September 11, 2015 17:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Avinash-Bhat/cc0dac9e842d44befdbd to your computer and use it in GitHub Desktop.
Retrofit 2 logging
OkHttpClient createClient() {
final OkHttpClient client = new OkHttpClient();
// ...
client.networkInterceptors().add(chain -> {
Request request = chain.request();
if (DEBUG) {
log(request);
} else {
String protocolPrefix = request.isHttps() ? "S" : "";
Log.i(TAG, String.format("---> HTTP%s %s %s",
protocolPrefix, request.method(), request.urlString()));
}
long start = System.nanoTime();
Response response = chain.proceed(request);
long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
if (DEBUG) {
return log(request.urlString(), response, elapsedTime);
} else {
Log.i(TAG, String.format("<--- HTTP %s %s (%sms)",
response.code(), request.urlString(), elapsedTime));
}
return response;
});
// ...
return client;
}
void log(Request r) throws IOException {
final Request request = r.newBuilder().build();
String protocolPrefix = request.isHttps() ? "S" : "";
Log.d(TAG,
String.format("---> HTTP%s %s %s", protocolPrefix, request.method(),
request.urlString()));
Headers headers = request.headers();
for (String header : headers.names()) {
Log.d(TAG, String.format("%s: %s", header, headers.get(header)));
}
String bodySize = "no";
RequestBody body = request.body();
if (body != null) {
MediaType bodyMime = body.contentType();
if (bodyMime != null) {
Log.d(TAG, "Content-Type: " + bodyMime);
}
long bodyLength = body.contentLength();
bodySize = bodyLength + "-byte";
if (bodyLength != -1) {
Log.d(TAG, "Content-Length: " + bodyLength);
}
if (headers.size() > 0) {
Log.d(TAG, "\n");
}
Buffer sink = new Buffer();
body.writeTo(sink);
if (bodyMime != null) {
Log.d(TAG, sink.readUtf8());
}
}
Log.d(TAG, String.format("---> END HTTP%s (%s body)", protocolPrefix, bodySize));
}
private Response log(String url, Response response, long elapsedTime)
throws IOException {
Response.Builder builder = response.newBuilder();
Log.d(TAG, String.format("<--- HTTP %s %s (%sms)", response.code(), url, elapsedTime));
Headers headers = response.headers();
for (String header : headers.names()) {
Log.d(TAG, String.format("%s: %s", header, headers.get(header)));
}
ResponseBody body = response.body();
byte[] source = body.bytes();
MediaType mediaType = body.contentType();
builder.body(ResponseBody.create(mediaType, source));
if (headers.size() > 0) {
Log.d(TAG, "");
}
Charset defaultCharset = Charset.forName("UTF-8");
Charset charset = mediaType != null ? mediaType.charset() != null ? mediaType.charset()
: defaultCharset : defaultCharset;
Log.d(TAG, new String(source, charset));
Log.d(TAG, String.format("<--- END HTTP (%s-byte body)", body.contentLength()));
return builder.build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment