一、细粒度的配置Feign的日志级别(针对每个微服务配置)
1、java代码方式
(1)在Feign接口注解上面配置configuration
/**
* @author : maybesuch
* @version : 1.0
* @Description : 用户中心Feign接口
* @Date : 2020/1/9 11:43
* @Copyright : Copyright (c) 2020 All Rights Reserved
**/
@FeignClient(value = "user-center", configuration = UserCenterFeignConfiguration.class)
public interface UserCenterFeignClient { @GetMapping("/users/{id}")
UserDTO findById(@PathVariable("id") Integer id); }
(2)定义configuration内容,也就是feign的日志级别
Logger有四种类型:NONE(默认)
、BASIC
、HEADERS
、FULL
,通过注册Bean来设置日志记录级别
注意:在此方法上不需要@Configuration注解,否则会被所有的FeignClient共享,如果添加了注解,则需要将此类放到启动时扫描不到的包
/**
* @author : maybesuch
* @version : 1.0
* @Description : 定义请求用户中心Feign的日志级别
* @Date : 2020/1/9 17:24
* @Copyright : Copyright (c) 2020 All Rights Reserved
**/
public class UserCenterFeignConfiguration { @Bean
Logger.Level feignLoggerLevel(){
// 设置日志
return Logger.Level.FULL;
}
}
(3)将Feign的全路径在application.yml中配置
logging:
level:
# com.maybesuch.contentcenter.feignclient.UserCenterFeignClient: debug
com.maybesuch: debug
2、配置文件的方式
只需在application.yml中添加配置:
logging:
level:
# com.maybesuch.contentcenter.feignclient.UserCenterFeignClient: debug
com.maybesuch: debug feign:
client:
config:
# 要调用服务的名称
user-center:
loggerLevel: full
二、全局日志级别的配置
1、java代码的方式
(1)在启动类@EnableFeignClients注解上配置defaultConfiguration
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = GlobalFeignConfiguration.class)
public class ContentCenterApplication { public static void main(String[] args) {
SpringApplication.run(ContentCenterApplication.class, args);
}
}
(2)定义GlobalFeignConfiguration类,此类也不需要@Configuration注解
/**
* @author : maybesuch
* @version : 1.0
* @Description : 定义全局Feign的日志级别
* @Date : 2020/1/9 17:24
* @Copyright : Copyright (c) 2020 All Rights Reserved
**/
public class GlobalFeignConfiguration { @Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
(3)配置文件application.yml中需配置日志级别方能打印出Feign调用的日志信息
logging:
level:
com.maybesuch: debug
2、配置文件方式
只需在application.yml中添加配置:
logging:
level:
com.maybecare: debug feign:
client:
config:
# feign全局日志级别
default:
loggerLevel: full
日志打印结果:
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] ---> GET http://user-center/users/1 HTTP/1.1
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] ---> END HTTP (0-byte body)
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] <--- HTTP/1.1 200 (17ms)
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] content-type: application/json;charset=UTF-8
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] date: Fri, 10 Jan 2020 05:33:40 GMT
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] transfer-encoding: chunked
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById]
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] {"id":1,"bane":"maybesuch"}
c.m.c.feignclient.UserCenterFeignClient : [UserCenterFeignClient#findById] <--- END HTTP (161-byte body)