001package io.freefair.spring.okhttp.logging;
002
003import io.freefair.spring.okhttp.ApplicationInterceptor;
004import io.freefair.spring.okhttp.OkHttp3AutoConfiguration;
005import okhttp3.logging.HttpLoggingInterceptor;
006import org.springframework.beans.factory.ObjectProvider;
007import org.springframework.boot.autoconfigure.AutoConfiguration;
008import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
009import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
010import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
011import org.springframework.boot.context.properties.EnableConfigurationProperties;
012import org.springframework.context.annotation.Bean;
013
014/**
015 * @author Lars Grefer
016 */
017@AutoConfiguration(before = OkHttp3AutoConfiguration.class)
018@ConditionalOnClass(HttpLoggingInterceptor.class)
019@EnableConfigurationProperties(OkHttp3LoggingInterceptorProperties.class)
020public class OkHttp3LoggingInterceptorAutoConfiguration {
021
022    @Bean
023    @ApplicationInterceptor
024    @ConditionalOnMissingBean
025    @ConditionalOnProperty(value = "okhttp.logging.enabled", havingValue = "true", matchIfMissing = true)
026    public HttpLoggingInterceptor okHttp3LoggingInterceptor(
027            OkHttp3LoggingInterceptorProperties properties,
028            ObjectProvider<HttpLoggingInterceptor.Logger> logger
029    ) {
030        HttpLoggingInterceptor.Logger actualLogger = logger.getIfUnique(() -> HttpLoggingInterceptor.Logger.DEFAULT);
031
032        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(actualLogger);
033
034        httpLoggingInterceptor.setLevel(properties.getLevel());
035
036        return httpLoggingInterceptor;
037    }
038}