Retrofit — Using the Log Level to Debug Requests

After diving deep into the art of using Retrofit for network requests, we'll look in this post at the debugging capabilities of Retrofit. Let's review the covered topics in our Retrofit series.

Retrofit Series Overview

  1. Introduction to Call Adapters (Coming soon)
  2. Custom Call Adapter to Separate OnResponse Callback (Coming soon)
  3. How to Integrate RxJava 1.x Call Adapter (Coming soon)
  4. How to Integrate RxJava 2.x Call Adapter (Coming soon)
  5. How to Integrate Guava Call Adapter (Coming soon)
  6. Custom Call Adapter to Separate Network and Gson Errors (Coming soon)
  1. Callbacks (Coming soon)
  2. Annotations (Coming soon)
  3. Fluent Interface with Builders (Coming soon)

Logging in Retrofit 2

We’ve published a blog post on how to debug requests using Retrofit 2. If you already jumped on the second major version of Retrofit, please follow the link to the related post.

Request Logging

As you can see by our long list of published posts, we at Future Studio are fans of Retrofit. Just a few days ago we utilized another great feature of Retrofit: the request logging.

If you're having a similar problem and trouble understanding why a request from your Android app does not work the way it is intended, follow this guide for easy trouble shooting (or, at the very least, to be able to blame the API devs).

Activating Retrofit Logging

That we can view the requests and responses can be a nice assistant. However, it's deactivated by default. Luckily, enabling the logging feature is fast and easy:

RestAdapter.Builder builder = new RestAdapter.Builder()  
    .setEndpoint(API_LOCATION)
    .setLogLevel(RestAdapter.LogLevel.FULL) // this is the important line
    .setClient(new OkClient(new OkHttpClient()));

This is all you've to do. Now start the app or the tests and force an execution of the requests in question.

Using the Logs

After the requests were made, take a look at the Android Logcat of your testing device. Retrofit posts a bunch of interesting things:

Retrofit's Logs using Log Level FULL

As you can see, this includes the entire request and response body. While this can be useful and necessary, the information can be too much and clutter up your log.

Knowing the Levels

Not to worry, Retrofit has different logging levels to match the amount of required information without blowing up your logs too much. Let's take a look at the various levels:

  • NONE

Description: No logging.

  • BASIC

Description: Log only the request method and URL and the response status code and execution time.

Example:

Retrofit's Logs using BASIC

  • HEADERS

Description: Log the basic information along with request and response headers.

Example:

Retrofit's Logs using HEADERS

  • HEADERS_AND_ARGS

Description: Log the basic information along with request and response objects via toString().

Example:

Retrofit's Logs using HEADERS_AND_ARGS

  • FULL

Description: Log the headers, body, and metadata for both requests and responses.

Example:

see above at Using the Logs

Utilize Logging

We hope this post gave you a short introduction into a feature of Retrofit, which can be tremendously helpful. Keep it in your mind if you want to debug a specific request or just log all the networking your app is doing.

If you've questions, please let us know at @futurestud_io.


Additional Resources


Explore the Library

Find interesting tutorials and solutions for your problems.

Miscellaneous