Retrofit 2 — Enable Logging for Development Builds Only

In a previous blog post we've shown you how to enable logging of requests and responses with Retrofit 2. We also explained that you should only enable logging for development builds only. We recommended that production versions should have logging disabled. In this blog post we'll show you how to automate this process.

Before you go on, you might want to take a look at all the other Retrofit topics we've covered in the past:

Retrofit Series Overview

Differentiate Development/Production Builds

Automation is one of the best tools to increase developer focus and productivity. Enabling and disabling logging for Retrofit can be one of the tedious, repetitive tasks which steal your attention. Furthermore, it increases the chance that the logging is still enabled for a production build. So let's automate this process: logging will be enabled for debug builds during your development process; and logging will be disabled for all production versions of your app!

The solution is pretty simple: we'll utilize the BuildConfig.DEBUG boolean variable which is provided by the Android framework. It will return true for your development builds and false for your production builds.

In the previous blog post we've shown you this code snippet:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
logging.setLevel(Level.BODY);

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
httpClient.addInterceptor(logging);

Retrofit retrofit = new Retrofit.Builder()  
   .baseUrl(API_BASE_URL)
   .addConverterFactory(GsonConverterFactory.create())
   .client(httpClient.build())
   .build();

It's time to slightly change the code to only enable logging for debug builds:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

if (BuildConfig.DEBUG) {  
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(Level.BODY);

    httpClient.addInterceptor(logging);
}

Retrofit retrofit = new Retrofit.Builder()  
   .baseUrl(API_BASE_URL)
   .addConverterFactory(GsonConverterFactory.create())
   .client(httpClient.build())
   .build();

This will only add the logging intercepter if it's a development build. We highly recommend applying this or a similar approach to your app. It's not going to be hours, but it's going to save you a little time every day. Your time matters!

Use Different Logging Levels

If you see value in logging even for production apps, but want to use a different logging level, you can use the approach we've just shown you.

Let's modify the code snippet one more time:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

if (BuildConfig.DEBUG) {  
    // development build
    logging.setLevel(Level.BODY);
} else {
    // production build
    logging.setLevel(Level.BASIC);
}

httpClient.addInterceptor(logging);

Retrofit retrofit = new Retrofit.Builder()  
   .baseUrl(API_BASE_URL)
   .addConverterFactory(GsonConverterFactory.create())
   .client(httpClient.build())
   .build();

Outlook

In this blog post, you've learned how to differentiate between development and production builds of your app. You can use this for various purposes, but here we've utilized the information to enable logging for debugging builds while disabling or reducing the logging level for production builds.

Explore the Library

Find interesting tutorials and solutions for your problems.