Retrofit logging

How to log requests and responses to the console

For a more complete and in-depth explanation, check out our complete Retrofit course

Download the starter project here

Github repository

Logging in Retrofit is done by intercepting the requests that Retrofit performs and printing them out to the console. That would include the requests that are being sent, the data, so the headers and all the information that is being sent with the request, and the reply, the information that is received from the backend api.

This is useful when developing since if we are not getting the expected information, we might want to check the logs to see what is transmitted and received.

Logging intercepts the requests and responses and logs them to the console. They can also be sent to a log file. In this tutorial we will focus on logging to the console.

Levels

There are 4 levels of logging

  • BASIC will log the request and response lines, so the endpoint that is called and what is received.
  • HEADERS will log everything that the BASIC level logs + the headers that are transmitted and received
  • BODY will log everything that the HEADERS level logs + the data that is sent and received
  • NONE will log nothing

Logging should only be done for development builds, we don’t want the production build to have any sort of logging information or anything that’s not required for the application to run. Not least because production builds can use Proguard that obfuscates the code, so logging will not work or will require more effort to make work in production builds.

In this tutorial I will show you a small trick to separate the logging one for development builds and not for production builds.

Building the logs

Firstly you need to import the logging library. Open your app level build.gradle file and add the following line in the dependencies block

implementation “com.squareup.okhttp3:logging-interceptor:4.4.0”

OkHttp3 is the low level library that Retrofit uses in order to perform its operations.

Updating the service

Add the logger creation code to the top of the Service class

val okHttp3Client = OkHttpClient.Builder()

init {

    val logging = HttpLoggingInterceptor()

    logging.level = HttpLoggingInterceptor.Level.BODY

    okHttp3Client.addInterceptor(logging)

}

Update the api creation functionality by adding the following code

.client(okHttp3Client.build())

The final class should look like this

object ApiCallService {

    private val BASE_URL = “https://us-central1-apis2-e78c3.cloudfunctions.net/”

    val okHttp3Client = OkHttpClient.Builder()

    init {

        val logging = HttpLoggingInterceptor()

        logging.level = HttpLoggingInterceptor.Level.BODY

        okHttp3Client.addInterceptor(logging)

    }

    private val api =

        Retrofit.Builder()

            .baseUrl(BASE_URL)

            .addConverterFactory(GsonConverterFactory.create())

            .client(okHttp3Client.build())

            .build()

            .create(ApiCall::class.java)

    fun call() = api.callGet()

}

Running the code will give you the logging output to the console

For a more complete and in-depth explanation, check out our complete Retrofit course

Close Bitnami banner
Bitnami