我正在为 REST API 开发 JAVA 客户端。我正在为客户使用 Retrofit。我看到我可以在改造中创建适配器时设置日志级别。所有这些日志当前都转到控制台。但是,我想将其重定向到已在应用程序中使用的 logback 生成的日志。我该怎么做?
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(APP_URL)
.setRequestInterceptor(new AuthRequestInterceptor())
.setErrorHandler(new RetrofitErrorHandler()).build();
输出
---> HTTP GET http://localhost:8080/services/v1/countries
Auth-Token: ...
---> END HTTP (no body)
<--- HTTP 200 http://localhost:8080/services/v1/countries (448ms)
Transfer-Encoding: chunked
: HTTP/1.1 200 OK
Vary: Accept-Encoding
Date: Thu, 04 Jun 2015 01:36:29 GMT
Content-Type: application/json
...
<--- END HTTP (9130-byte body)
我希望所有这些都进入记录器日志。
最佳答案
RestAdapter.Builder
也支持传递 Log使用 setLog方法。 Log
只是一个接口(interface)。创建您自己的该接口(interface)的 Logback 实现,并在创建 RestAdapter
时将其传递给构建器。
public class LogbackLog implements Log {
public void log(String message) {
// call logback logger from here
}
}
Log logger = new LogbackLog();
RestAdapter restAdapter = new RestAdapter.Builder()
.setLog(logger)
.setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(APP_URL)
.setRequestInterceptor(new AuthRequestInterceptor())
.setErrorHandler(new RetrofitErrorHandler()).build();
关于java - 记录改造到 Logback 记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30633727/