设置feign配置日志级别
一、使用java编码的方式
基于《<spring-cloud.version>Greenwich.RC2</spring-cloud.version>》
根据spring cloud文档: https://cloud.spring.io/spring-cloud-static/Greenwich.RC2/single/spring-cloud.html#_feign_logging
feign日志
在创建feign client的时候,就创建了logger, 默认logger的名称是创建feign client的服务接口类的全路径,通俗的来讲就是加了@FeignClient接口类的全路径
首先在application.yml文件中指定日志级别:
logging:
level:
com.**.api.service.IFeignService: DEBUG
设置日志记录的级别
logger有四种类型:NONE,BASIC,HEADERS, FULL,通过注册Bean来设置日志记录级别:
@Configuration
public class LogConfiguration {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
在feign client加上:
@FeignClient(name = "IFeignService", configuration = LogConfiguration.class)
public interface IFeignService {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String invokeSayHi();
}
日志记录结果:
2018-12-31 c.g.v.f.c.api.service.IFeignService <--- HTTP/1.1 200 (462ms)
2018-12-31 c.g.v.f.c.api.service.IFeignService content-length: 5
2018-12-31 c.g.v.f.c.api.service.IFeignService content-type: text/plain;charset=UTF-8
2018-12-31 c.g.v.f.c.api.service.IFeignService date: Mon, 31 Dec 2018 13:35:48 GMT
2018-12-31 c.g.v.f.c.api.service.IFeignService
2018-12-31 c.g.v.f.c.api.service.IFeignService index
2018-12-31 c.g.v.f.c.api.service.IFeignService <--- END HTTP (5-byte body)
内容参考
https://blog.csdn.net/u013887008/article/details/85489631
缺点:
如上方法,针对每一个有@FeignClient注解的地方都要增加configuration配置,项目大了,不好到处修改。
二、使用配置文件的方式
在application.yml文件中全局配置
logging:
level:
# com.maybesuch.contentcenter.feignclient.UserCenterFeignClient: debug
com.maybesuch: debug
feign:
client:
config:
# 要调用服务的名称
user-center:
loggerLevel: full
缺点同上,有多少个调用服务名称,就需要配置多个。
参考https://www.cnblogs.com/maybesuch/p/12175002.html
重写feign.logger类,并加入配置
则可以使用如下方法
比如打印info信息级别的日志
1、重写feign.logger类
public class QjxFeignLogger extends feign.Logger {
private final Logger logger;
public QjxFeignLogger() {
this(feign.Logger.class);
}
public QjxFeignLogger(Class<?> clazz) {
this(LoggerFactory.getLogger(clazz));
}
public QjxFeignLogger(String name) {
this(LoggerFactory.getLogger(name));
}
QjxFeignLogger(Logger logger) {
this.logger = logger;
}
@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 configKey, String format, Object... args) {
// Not using SLF4J's support for parameterized messages (even though it
// would be more efficient) because it would
// require the incoming message formats to be SLF4J-specific.
if (logger.isInfoEnabled()) {
logger.info(String.format(methodTag(configKey) + format, args));
}
}
}
自定义一个Logger类,继承Feign.Logger,将代码中的Debug修改成为Info
2、自定义Logger类加入配置
@Configuration
public class FeignClientConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
Logger QjxFeign(){
return new QjxFeignLogger();
}
}
将自定义Logger类加入到Feign客户端配置的Config中,这样Feign系统间调用就能打印出Info级别的日志。
转载自 https://www.cnblogs.com/lkd934/p/9541417.html
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程