All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yandex.disk.rest.LoggingInterceptor Maven / Gradle / Ivy

/*
* Copyright (c) 2015 Yandex
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.yandex.disk.rest;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/* package */ class LoggingInterceptor implements Interceptor {

    private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);

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

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

        Response src = chain.proceed(request);
        Response response = src.newBuilder()
                .addHeader("Debug-Code", String.valueOf(src.code()))
                .addHeader("Debug-Message", src.message())
                .build();

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

        return response;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy