5

Previous version of Retrofit uses RestAdapter and has provision of enabling the Logs. Why that feature is removed in Retrofit 2.0?

To enable log, I have to do..

 Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    /** Handles Log */
    retrofit.client().interceptors().add(new LoggingInterceptor());


class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Logger.d(String.format("Sending request %s on %s%n%s",
            request.url(), chain.connection(), request.headers()));

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    Logger.d(String.format("Received response for %s in %.1fms%n%s",
            response.request().url(), (t2 - t1) / 1e6d, response.headers()));

   // Logger.d(""+new String(response.body().bytes()));
    return response;
}

This is the Only solution for this? Previous provision was very handy...

sreekumar
  • 2,439
  • 1
  • 21
  • 27
  • Possible duplicate of [Logging with Retrofit 2](http://stackoverflow.com/questions/32514410/logging-with-retrofit-2) – bryant1410 Nov 25 '15 at 18:05

4 Answers4

2

Intention of Retrofit is to perform type safe de/serialization. Probably it has dropped features that should be performed by the http client, like it is the logging.

Reasonably, the http client should be logging the responses received rather than Retrofit. Your question is too broad, the guys from Square should add more.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
2

For logging in retrofit 2 okhttp has the following logger:

 private OkHttpClient getOkHttpClient() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    if (BuildConfig.LOG_HTTP_CALLS) {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    }

    return new OkHttpClient.Builder()
            .addInterceptor(logging).build();
}

and the dependency in build.gradle is

compile 'com.squareup.okhttp3:logginginterceptor:2.1.0'

UDI
  • 1,602
  • 1
  • 14
  • 13
0
OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    if (BuildConfig.DEBUG) {
                        Log.e(getClass().getName(), request.method() + " " + request.url());
                        Log.e(getClass().getName(), "" + request.header("Cookie"));
                        RequestBody rb = request.body();
                        Buffer buffer = new Buffer();
                        if (rb != null)
                            rb.writeTo(buffer);
                        LogUtils.LOGE(getClass().getName(), "Payload- " + buffer.readUtf8());
                    }
                    return chain.proceed(request);
                }
            })
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .build();

            iIdgardServices = new Retrofit.Builder()
                    .baseUrl("Your Base URL")
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(Your Service Interface .class);

Works for Me

Nilesh Senta
  • 5,236
  • 2
  • 19
  • 27
0
private static final String HOST = Urls.HOST;

private static final OkHttpClient.Builder mHttpClientBuilder = new OkHttpClient.Builder();

static {
    //add this line PS: `Level.NONE` is close
    mHttpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor(new EncryptInterceptor())
            .connectTimeout(30, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .addNetworkInterceptor(new TokenInterceptor())
            .authenticator(new DefaultAuthenticator());
}

public static <S> S createService(Class<S> serviceClass) {
    OkHttpClient client = mHttpClientBuilder.build();
    Retrofit retrofit = mBuilder.client(client).build();
    return retrofit.create(serviceClass);
}

change the logcat to DEBUG can see the log!

Fang
  • 3,652
  • 4
  • 16
  • 30