美文网首页
feign日志处理

feign日志处理

作者: Jabwin | 来源:发表于2019-06-29 09:46 被阅读0次

    使用了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日志处理

          本文链接:https://www.haomeiwen.com/subject/gopucctx.html