使HttpLoggingInterceptor不记录图像

问题描述 投票:2回答:1

我正试图摆脱日志中的垃圾,比如

* $ʞx J/

当我收到一张图片

所以我试图覆盖HttpLoggingInterceptor intercept(),以检测在响应中是否有Content-Type => image / jpeg标头,但是HttpLoggingInterceptor是最终的,所以我无法扩展它:(

RetrofitModule中的代码:

OkHttpClient provideOkHttpClient(Context context, Application app, Preferences preferences) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.HEADERS);

        Cache cache = new Cache(app.getCacheDir(), cacheSize);

        return new OkHttpClient.Builder()
                .addNetworkInterceptor(new OkHttpInterceptor(context, preferences))
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .build();
    }

如何在我的项目中禁用图像记录?

android logging retrofit okhttp
1个回答
1
投票

所以,既然没有人有答案,我发明自己的自行车:

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
        @Override
        public void log(String message) {
            if(!message.contains("�")){
                Timber.d(message);
            }
        }
    });

不确定String.contains()是否足够便宜,可以像这样使用它,但是达到了目标

© www.soinside.com 2019 - 2024. All rights reserved.