【问题标题】:Logging Retrofit to Logback Logger日志改造到 Logback 记录器
【发布时间】:2015-06-04 01:59:28
【问题描述】:

我正在为 REST API 开发一个 JAVA 客户端。我正在为客户使用改造。我看到我可以在改造中创建适配器时设置日志级别。所有这些日志当前都进入控制台。但是,我想将此重定向到应用程序中已使用的 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)

我希望所有这些都进入记录器日志。

【问题讨论】:

    标签: java logging slf4j logback retrofit


    【解决方案1】:

    RestAdapter.Builder 还支持使用setLog 方法将Log 传递给它。 Log 只是一个接口。创建您自己的该接口的 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();
    

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多