美文网首页springbootfeign
Feign Logging:Feign日志记录

Feign Logging:Feign日志记录

作者: zlup | 来源:发表于2017-06-30 13:55 被阅读2275次

    每一个被创建的Feign客户端都会有一个logger。该logger默认的名称为Feign客户端对应的接口的全限定名。Feign日志记录只能响应DEBUG日志级别。

    示例:

    1. 在Spring Boot项目中,设置Feign客户端的日志级别。

    application.yml

    logging:

      level:

        com:

          example:

            demo:

              service:

                 ServiceHello:debug

    2. Feign客户端ServiceHello接口代码。

    package com.example.demo.service;

    import com.example.demo.config.ServiceHelloConfiguration;

    import com.example.demo.hystrix.ServiceHelloFallback;

    import org.springframework.cloud.netflix.feign.FeignClient;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RequestMethod;

    import org.springframework.web.bind.annotation.RequestParam;

    @FeignClient(value="service-hello",configuration= ServiceHelloConfiguration.class, fallback= ServiceHelloFallback.class)

    public interface ServiceHello {

    @RequestMapping(value="/hello", method= RequestMethod.GET)

    String sayHelloFromProvider(@RequestParam(value="name") String name);

    }

    3. 在Feign客户端配置类ServiceHelloConfiguration中配置Feign日志记录详细信息。

    package com.example.demo.config;

    import feign.Logger;

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    @Configuration

    public class ServiceHelloConfiguration {

    @Bean

    Logger.Level feignLoggerLevel() {

    return Logger.Level.HEADERS;

      }

    }

    4. 针对每一个Feign客户端,可以配置一个Logger.Level对象,通过该对象控制日志输出内容。

    Logger.Level有如下几种选择:

    NONE, 不记录日志 (默认)。

    BASIC, 只记录请求方法和URL以及响应状态代码和执行时间。

    HEADERS, 记录请求和应答的头的基本信息。

    FULL, 记录请求和响应的头信息,正文和元数据。

    5. Feign客户端日志样式。

    浏览器请求:http://localhost:8763/hello?name=kevin

    控制台查看对应日志(Logging.Level.HEADERS)

    相关文章

      网友评论

        本文标题:Feign Logging:Feign日志记录

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