feign日志处理

使用了slf4j来输出日志。
FeignConfig.java

import feign.Logger;
import feign.Request;
import feign.Response;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
/*************************************************************
 * Feign日志配置类:
 * @author Jabwin
 *************************************************************
 */
@EnableFeignClients
@Configuration
public class FeignConfig
{
    @Value("${feign.logout}")
    private boolean isLogOut;

    @Bean
    Logger.Level feignLoggerLevel()
    {
        return Logger.Level.FULL;
    }

    @Bean
    Logger myFeign()
    {
        return new Logger()
        {
            private final org.slf4j.Logger logger = LoggerFactory.getLogger(org.slf4j.Logger.class);
            @Override
            protected void logRequest(String configKey, Level logLevel, Request request)
            {
                if (logger.isInfoEnabled())
                {
                    super.logRequest(configKey, logLevel, request);
                }
            }

            @Override
            protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, long elapsedTime)
                    throws IOException
            {
                if (logger.isInfoEnabled())
                {
                    return super.logAndRebufferResponse(configKey, logLevel, response, elapsedTime);
                }
                return response;
            }
            @Override
            protected void log(String s, String s1, Object... objects)
            {
                if (isLogOut && logger.isInfoEnabled())
                {
                    logger.info(String.format(methodTag(s) + s1, objects));
                }
            }
        };
    }
}

application.properties

# feign配置
feign.httpclient.enabled=true
# 暂时关闭熔断器
feign.hystrix.enabled=false
feign.logout=true
# ribbon配置
ribbon.eureka.enabled=true
ribbon.ConnectTimeout=60000
ribbon.ReadTimeout=60000
# ribbon策略
ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.ZoneAvoidanceRule

# 关闭重试
ribbon.OkToRetryOnAllOperations=false

使用:在feign的接口@FeignClient注解中上加入configuration = {FeignConfig.class}

你可能感兴趣的:(feign日志处理)