Skip to content
Advertisement

Logging with Retrofit 2

I’m trying to get the exact JSON that is being sent in the request. Here is my code:

JavaScript

But I only see this in the logs:

JavaScript

How am I supposed to do proper logging, given the removal of setLog() and setLogLevel() which we used to use with Retrofit 1?

Advertisement

Answer

In Retrofit 2 you should use HttpLoggingInterceptor.

Add dependency to build.gradle. Latest version as of October 2019 is:

JavaScript

Create a Retrofit object like the following:

JavaScript

In case of deprecation warnings, simply change setLevel to:

JavaScript

The above solution gives you logcat messages very similar to the old ones set by

JavaScript

In case of java.lang.ClassNotFoundException:

Older Retrofit version might require an older logging-interceptor version. Take a look at comments sections for details.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement