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
- Getting Started and Creating an Android Client
- Basics of API Description
- Creating a Sustainable Android Client
- URL Handling, Resolution and Parsing
- How to Change API Base Url at Runtime
- Multiple Server Environments (Develop, Staging, Production)
- Share OkHttp Client and Converters between Retrofit Instances
- Upgrade Guide from 1.9
- Beyond Android: Retrofit for Java Projects
- How to use OkHttp 3 with Retrofit 1
- Synchronous and Asynchronous Requests
- Send Objects in Request Body
- Add Custom Request Header
- Manage Request Headers in OkHttp Interceptor
- Dynamic Request Headers with @HeaderMap
- Multiple Query Parameters of Same Name
- Optional Query Parameters
- Send Data Form-Urlencoded
- Send Data Form-Urlencoded Using FieldMap
- How to Add Query Parameters to Every Request
- Add Multiple Query Parameter With QueryMap
- How to Use Dynamic Urls for Requests
- Constant, Default and Logic Values for POST and PUT Requests
- Cancel Requests
- Reuse and Analyze Requests
- Optional Path Parameters
- How to Send Plain Text Request Body
- Customize Network Timeouts
- How to Trust Unsafe SSL certificates (Self-signed, Expired)
- Dynamic Endpoint-Dependent Interceptor Actions
- How to Update Objects on the Server (PUT vs. PATCH)
- How to Delete Objects on the Server
- Introduction to (Multiple) Converters
- Adding & Customizing the Gson Converter
- Implementing Custom Converters
- How to Integrate XML Converter
- Access Mapped Objects and Raw Response Payload
- Supporting JSON and XML Responses Concurrently
- Handling of Empty Server Responses with Custom Converter
- Send JSON Requests and Receive XML Responses (or vice versa)
- Unwrapping Envelope Responses with Custom Converter
- Wrapping Requests in Envelope with Custom Converter
- Define a Custom Response Converter
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.