Search code examples
androidkotlinloggingretrofitokhttp

Make HttpLoggingInterceptor do not log images


I'm trying to get rid of trash in logs, like

*�$ʞx���J/

when i recieve an image

So i tried to override HttpLoggingInterceptor intercept(), to detect is there a Content-Type => image/jpeg header in responce, but HttpLoggingInterceptor is final so i cant extend it :(

Code in 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();
    }

How can i disable image-logging in my project?


Solution

  • So, since no one have an answer iv'e invent my own bicycle:

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

    Not really sure if String.contains() cheap enough for use it like this, but goal is reached