其他分享
首页 > 其他分享> > 介绍OpenFeign日志打印功能

介绍OpenFeign日志打印功能

作者:互联网

  OpenFegin的日志打印功能是什么?Feign提供了日志打印功能,可以通过配置来调整日志级别,从而了解Feign执行Http请求的细节。通俗地说,就是对Feign接口的调用情况进行监控和输出。

日志级别

配置日志

@Configuration
public class FeignLogConfiguration {

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

  FeignLogConfiguration类上添加了@Configuration注解,说明是全局配置,若注释掉此注解,则开启了局部配置。在yaml文件中配置哪个接口需要以什么日志级别进行监控:


 logging.level.com.swagger.demo.api.GithubFeignClient = DEBUG

  之所以把Feign接口的日志级别设置成DEBUG,是因为Feign的Logger.Level只对DEBUG作出响应。测试用例复用《Spring注解之@FeignClient注解使用方法》中的wiener/github/listContributors,依然调用 GithubFeignClient#listContributors。如果开启局部配置,就需要使用如下Client:

import com.swagger.demo.bean.Contributor;
import com.swagger.demo.config.FeignLogConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@FeignClient(name = "github", url = "${self.github.url}",
        configuration = FeignLogConfiguration.class,
        fallback = GithubFeignClient.DefaultFallback.class)
public interface GithubFeignClient {

    @GetMapping(value = "/repos/{owner}/{repo}/contributors")
    ResponseEntity<List<Contributor>> listContributors(
            @PathVariable("owner") String owner,
            @PathVariable("repo") String repo);

    /**
     * 容错处理类,当调用失败时,简单返回空list
     */
    @Component
    class DefaultFallback implements GithubFeignClient {
        @Override
        public ResponseEntity<List<Contributor>> listContributors(
                @PathVariable("owner") String owner,
                @PathVariable("repo") String repo) {
            return new ResponseEntity(new ArrayList<>(), HttpStatus.BAD_REQUEST);
        }
    }

}

后台日志查看

  切换FeignLogConfiguration中的日志级别并启动项目,访问wiener/github/listContributors,则可以看到内容不同的日志。

2022-02-10 20:20:16.792 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] ---> GET https://api.github.com/repos/openFeign/feign/contributors HTTP/1.1
2022-02-10 20:20:16.792 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] ---> END HTTP (0-byte body)
2022-02-10 20:20:17.119 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] <--- HTTP/1.1 200 OK (326ms)
2022-02-10 20:20:17.119 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] accept-ranges: bytes
2022-02-10 20:20:17.119 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] access-control-allow-origin: *
2022-02-10 20:20:17.119 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] access-control-expose-headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
...
2022-02-10 20:20:17.120 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] x-ratelimit-reset: 1644496768
2022-02-10 20:20:17.120 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] x-ratelimit-resource: core
2022-02-10 20:20:17.120 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] x-ratelimit-used: 11
2022-02-10 20:20:17.120 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] x-xss-protection: 0
2022-02-10 20:20:17.272 DEBUG 15199 --- [nio-8087-exec-9] com.swagger.demo.api.GithubFeignClient   : [GithubFeignClient#listContributors] <--- END HTTP (28116-byte body)

Reference

标签:listContributors,20,OpenFeign,GithubFeignClient,打印,import,日志,com
来源: https://www.cnblogs.com/east7/p/15941597.html