Java 假日志不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42751269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Feign logging not working
提问by Magick
I'm trying to get logging working for each request from a Feign rest client. However I cannot get the logging to work, while 'standard' Slf4j logging does work.
我正在尝试为来自 Feign 休息客户端的每个请求进行日志记录。但是,我无法使日志记录工作,而“标准”Slf4j 日志记录确实有效。
I have the following:
我有以下几点:
public MyClient() {
initConnectionProperties();
this.service = Feign.builder()
.contract(new JAXRSContract())
.decoder(getHymansonDecoder())
.encoder(getHymansonEncoder())
.requestInterceptor(new BasicAuthRequestInterceptor(user, password))
//.client(new OkHttpClient())
.logger(new Slf4jLogger(MyClient.class)) //not working
.logLevel(feign.Logger.Level.BASIC)
.target(MyClient.class, this.url);
logger.info("Connection parameters: url = " + url + ", user = " + user); //Is working
}
回答by Stimp
You may also need to configure your log4j logging level for feign
to DEBUG. If you are using spring boot, what worked for me is:
您可能还需要为feign
DEBUG配置 log4j 日志记录级别。如果您使用的是弹簧靴,那么对我有用的是:
curl -X POST http://localhost/loggers/feign -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}'
回答by Maverick
You need to configure logging in application.properties as below:
您需要在 application.properties 中配置日志记录,如下所示:
logging.level.<package path>.MyClient=DEBUG
If you're using application.yml then:
如果您使用的是 application.yml,则:
logging.level.<package path>.MyClient: DEBUG
The log level can be set to tell Feign how much to log.
可以设置日志级别来告诉 Feign 要记录多少。
Options are:
选项是:
- NONE, No logging (DEFAULT)
- BASIC, Log only the request method and URL and the response status code and execution time
- HEADERS, Log the basic information along with request and response headers
- FULL, Log the headers, body, and metadata for both requests and responses
- NONE,无日志记录(默认)
- BASIC,只记录请求方法和 URL 以及响应状态码和执行时间
- HEADERS,记录基本信息以及请求和响应头
- FULL,记录请求和响应的标头、正文和元数据
Example:
例子:
logLevel(feign.Logger.Level.NONE)
or
logLevel(feign.Logger.Level.BASIC)
or
logLevel(feign.Logger.Level.HEADERS)
or
logLevel(feign.Logger.Level.FULL)
For more details, you can refer this
有关更多详细信息,您可以参考这个
回答by Jean-Alexis Aufauvre
private void setup() {
//...
feignBuilder.logger(new MyLogger());
feignBuilder.logLevel(Logger.Level.FULL);
}
private static class MyLogger extends Logger {
@Override
protected void log(String s, String s1, Object... objects) {
System.out.println(String.format(s + s1, objects)); // Change me!
}
}
回答by Niraj Sonawane
This is how i was able to log using Custom Config class
这就是我能够使用自定义配置类登录的方式
NoteFeign logging only responds to the DEBUG level.
注意Feign logging 仅响应 DEBUG 级别。
Config Class
配置类
@Configuration
public class UserClientConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.HEADERS;
}
}
Client
客户
@FeignClient(name = "User", url = "http://localhost:8080",configuration=UserClientConfig.class)
public interface UserClient {
@RequestMapping(method = RequestMethod.GET, value = "/user")
List<User> getAllUsers();
}
application.properties
应用程序属性
logging.level.<pcakgepath>.UserClient: DEBUG
回答by Roland
First you need to set the logging level for your feign client class to DEBUG, as Maverick already mentioned in his answer.
首先,您需要将 feign 客户端类的日志记录级别设置为 DEBUG,正如 Maverick 在他的回答中已经提到的那样。
Then if you use Spring Boot, beside the option to create @Configuration classes as Niraj already mentioned in his answer, you can configure each client individually in you application properties/yml config file:
然后,如果您使用 Spring Boot,除了创建 @Configuration 类的选项之外,Niraj 在他的回答中已经提到,您可以在应用程序属性/yml 配置文件中单独配置每个客户端:
feign:
client:
config:
the_name_of_your_feign_client:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
feign:
client:
config:
the_name_of_your_feign_client:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
Or use default instead of the_name_of_your_feign_client to configure all your feign clients in the same way:
或者使用 default 而不是 the_name_of_your_feign_client 以相同的方式配置所有 feign 客户端:
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
回答by Tony Murphy
I hadn't set a client, and my client calls were failing, and logging wasn't working.. once I added OkHttpClient and changed logback.xml file, worked fine
我没有设置客户端,我的客户端调用失败,并且日志记录不起作用..一旦我添加了 OkHttpClient 并更改了 logback.xml 文件,工作正常
MyApi myApi = Feign.builder()
.client(new OkHttpClient())
.decoder(new HymansonDecoder())
.encoder(new HymansonEncoder())
.logger(new Slf4jLogger())
.logLevel(feign.Logger.Level.FULL)
.target(MyApi.class, "http://localhost:8082");
this is logback.xml
这是 logback.xml
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d %green([%thread]) %highlight(%level) %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<logger name="feign.Logger" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>