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.beans.factory.annotation.Autowired;
008import org.springframework.boot.autoconfigure.AutoConfigureBefore;
009import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
010import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
011import org.springframework.boot.context.properties.EnableConfigurationProperties;
012import org.springframework.context.annotation.Bean;
013import org.springframework.context.annotation.Configuration;
014
015/**
016 * @author Lars Grefer
017 */
018@Configuration(proxyBeanMethods = false)
019@ConditionalOnClass(HttpLoggingInterceptor.class)
020@AutoConfigureBefore(OkHttp3AutoConfiguration.class)
021@EnableConfigurationProperties(OkHttp3LoggingInterceptorProperties.class)
022public class OkHttp3LoggingInterceptorAutoConfiguration {
023
024    @Autowired
025    private OkHttp3LoggingInterceptorProperties properties;
026
027    @Bean
028    @ApplicationInterceptor
029    @ConditionalOnMissingBean
030    public HttpLoggingInterceptor okHttp3LoggingInterceptor(
031            ObjectProvider<HttpLoggingInterceptor.Logger> logger
032    ) {
033        HttpLoggingInterceptor httpLoggingInterceptor;
034
035        if (logger.getIfAvailable() != null) {
036            httpLoggingInterceptor = new HttpLoggingInterceptor(logger.getIfAvailable());
037        }
038        else {
039            httpLoggingInterceptor = new HttpLoggingInterceptor();
040        }
041
042        httpLoggingInterceptor.level(properties.getLevel());
043
044        return httpLoggingInterceptor;
045    }
046}