android - HttpLoggingInterceptor 不显示任何日志

标签 android retrofit2

尝试将 HttpLoggingInterceptor 与 Retrofit2 一起使用,但没有结果,任何请求/响应都不会显示任何日志。

我使用的是图书馆的版本:

compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'

我在其中生成 api 实例的代码:

public class ApiGenerator {

    private static final String BASE_URL = "http://api.openweathermap.org/data/2.5/";
    private static final String API_KEY = "de4d4a35830d98e785a1cbb06725457f";

    private ApiGenerator() {}

    public static WeatherApi createWeatherApi() {
        return createRetrofit().create(WeatherApi.class);
    }

    private static Retrofit createRetrofit() {
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(createClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    private static OkHttpClient createClient() {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder().addInterceptor(chain -> {
            Request request = chain.request();
            HttpUrl url = request.url().newBuilder()
                    .addQueryParameter("APPID", API_KEY)
                    .build();
            request.newBuilder().url(url).build();
            return chain.proceed(request);
        })
                .addInterceptor(logging)
                .build();
    }
}

最佳答案

您的示例使用的是您可能未配置的默认记录器。更好的解决方案是使用您选择的日志记录框架传入自定义记录器。 Example using SLF4J with Logback .

private static final Logger log = LoggerFactory.getLogger(HttpClient.class);

private static final HttpLoggingInterceptor loggingInterceptor =
    new HttpLoggingInterceptor((msg) -> {
        log.debug(msg);
    });
static {
    loggingInterceptor.setLevel(Level.BODY);
}

public static HttpLoggingInterceptor getLoggingInterceptor() {
    return loggingInterceptor;
}

关于android - HttpLoggingInterceptor 不显示任何日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44435562/

相关文章:

android - Google Play 内部测试不发送电子邮件

android - 使用 Retrofit2 和 rxJava2 处理没有主体的响应

android - 如何在 Kotlin 中使用 RetroFit2 responseBodyConverter?

android - Retrofit2-AWS S3 多部分图像文件损坏问题

retrofit2 - Retrofit 2.0中“A type-safe HTTP client for Android and Java”的含义

java - 无法从 REST API 获取 JSON 作为字符串

android - ffmpeg-分割参数列表时出错 : Option not found

android - 适用于 Android 的 sizeWithFont

java - 从 JSON 对象中获取第一个键和值

android - 找不到 Facebook SDK.apk