Retrofit 1 integrated a log feature for basic request and response debugging. The logging functionality was removed in Retrofit 2, since the required HTTP layer is now completely based on OkHttp. Since many developers asked for logging capabilities in Retrofit 2, the developers of OkHttp added a logging interceptor in release 2.6.0
. This post will show you how to add and use the logging interceptor in your Android app in combination with Retrofit 2.
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
Logging in Retrofit 1
We’ve published a blog post on how to debug requests using Retrofit 1. If you’re using the first major version of Retrofit, please follow the link to the related post.
Logging In Retrofit 2
Retrofit 2 completely relies on OkHttp for any network operation. The developers of OkHttp have released a separate logging interceptor project, which implements logging for OkHttp. You can add it to your project with a quick edit of your build.gradle
:
compile 'com.squareup.okhttp3:logging-interceptor:3.8.0'
While developing your app and for debugging purposes it’s nice to have a log feature integrated to show request and response information. Since logging isn’t integrated by default anymore in Retrofit 2, we need to add a logging interceptor for OkHttp. Luckily OkHttp already ships with this interceptor and you only need to activate it for your OkHttpClient.
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// add your other interceptors …
// add logging as last interceptor
httpClient.addInterceptor(logging); // <-- this is the important line!
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
We recommend to add logging as the last interceptor, because this will also log the information which you added with previous interceptors to your request.
Log Levels
Logging too much information will blow up your Android monitor, that’s why OkHttp’s logging interceptor has four log levels: NONE
, BASIC
, HEADERS
, BODY
. We’ll walk you through each of the log levels and describe their output.
None
No logging.
Use this log level for production environments to enhance your apps performance by skipping any logging operation.
Basic
Log request type, url, size of request body, response status and size of response body.
D/HttpLoggingInterceptor$Logger: --> POST /upload HTTP/1.1 (277-byte body)
D/HttpLoggingInterceptor$Logger: <-- HTTP/1.1 200 OK (543ms, -1-byte body)
Using the log level BASIC
will only log minimal information about your request. If you’re just interested in the request body size, response body size and response status, this log level is the right one.
Headers
Log request and response headers, request type, url, response status.
Using the HEADERS
log level will only log request and response headers. Retrofit or OkHttp will add appropriate request headers by default, but they won’t show up on your request since they are added later in the request chain. If you didn’t intercept the actual request on Android, there is nothing to see. If you add request headers yourself, make sure the logging interceptor is the last interceptor added to the OkHttp client. If you add the interceptor first, there isn’t any header data set on the request yet.
We use the two header fields Accept
and Content-Type
to illustrate the output if you define the values yourself.
D/HttpLoggingInterceptor$Logger: --> POST /upload HTTP/1.1
D/HttpLoggingInterceptor$Logger: Accept: application/json
D/HttpLoggingInterceptor$Logger: Content-Type: application/json
D/HttpLoggingInterceptor$Logger: --> END POST
D/HttpLoggingInterceptor$Logger: <-- HTTP/1.1 200 OK (1039ms)
D/HttpLoggingInterceptor$Logger: content-type: text/html; charset=utf-8
D/HttpLoggingInterceptor$Logger: cache-control: no-cache
D/HttpLoggingInterceptor$Logger: vary: accept-encoding
D/HttpLoggingInterceptor$Logger: Date: Wed, 28 Oct 2015 08:24:20 GMT
D/HttpLoggingInterceptor$Logger: Connection: keep-alive
D/HttpLoggingInterceptor$Logger: Transfer-Encoding: chunked
D/HttpLoggingInterceptor$Logger: OkHttp-Selected-Protocol: http/1.1
D/HttpLoggingInterceptor$Logger: OkHttp-Sent-Millis: 1446020610352
D/HttpLoggingInterceptor$Logger: OkHttp-Received-Millis: 1446020610369
D/HttpLoggingInterceptor$Logger: <-- END HTTP
Besides the headers of server’s response, you’ll get the information which protocol was selected and the respective milliseconds when your request was sent and the response was received.
Body
Log request and response headers and body.
This is the most complete log level and will print out every related information for your request and response.
D/HttpLoggingInterceptor$Logger: --> POST /upload HTTP/1.1
D/HttpLoggingInterceptor$Logger: --9df820bb-bc7e-4a93-bb67-5f28f4140795
D/HttpLoggingInterceptor$Logger: Content-Disposition: form-data; name="description"
D/HttpLoggingInterceptor$Logger: Content-Transfer-Encoding: binary
D/HttpLoggingInterceptor$Logger: Content-Type: application/json; charset=UTF-8
D/HttpLoggingInterceptor$Logger: Content-Length: 37
D/HttpLoggingInterceptor$Logger:
D/HttpLoggingInterceptor$Logger: "hello, this is description speaking"
D/HttpLoggingInterceptor$Logger: --9df820bb-bc7e-4a93-bb67-5f28f4140795--
D/HttpLoggingInterceptor$Logger: --> END POST (277-byte body)
D/HttpLoggingInterceptor$Logger: <-- HTTP/1.1 200 OK (1099ms)
D/HttpLoggingInterceptor$Logger: content-type: text/html; charset=utf-8
D/HttpLoggingInterceptor$Logger: cache-control: no-cache
D/HttpLoggingInterceptor$Logger: vary: accept-encoding
D/HttpLoggingInterceptor$Logger: Date: Wed, 28 Oct 2015 08:33:40 GMT
D/HttpLoggingInterceptor$Logger: Connection: keep-alive
D/HttpLoggingInterceptor$Logger: Transfer-Encoding: chunked
D/HttpLoggingInterceptor$Logger: OkHttp-Selected-Protocol: http/1.1
D/HttpLoggingInterceptor$Logger: OkHttp-Sent-Millis: 1446021170095
D/HttpLoggingInterceptor$Logger: OkHttp-Received-Millis: 1446021170107
D/HttpLoggingInterceptor$Logger: Perfect!
D/HttpLoggingInterceptor$Logger: <-- END HTTP (8-byte body)
This is the only log level where you’ll get the response body data. If you’re in an argument with your backend developer, use this log level to show the received response data. However, the BODY
log level will clutter your Android monitor if you’re receiving large data sets. Use this level only if necessary.
Outlook
We hope you can apply logging to your development flow and the shown information about the log level will help you choose the right one for you. Use different log levels and try what works best for you. Even if the BODY
log level might clutter up your log, maybe you’ll find your desired information only showing every request detail.
The next post will show you a how to integrate Hawk authentication on Android.