26

我使用 Retrofit 来进行 HTTP 请求和 JSON 解析,我喜欢打开调试日志的方式。日志允许查看正文请求、URL……这非常有用。由于改造使用 OkHttp,我想知道 OkHttp 是否也有办法为每个请求启用日志。

4

9 回答 9

34

使用Interceptor,您可以定义以下类:

class LoggingInterceptor implements Interceptor {
  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Log.d("OkHttp", String.format("Sending request %s on %s%n%s",
        request.url(), chain.connection(), request.headers()));

    Response response = chain.proceed(request);

    long t2 = System.nanoTime();
    Log.d("OkHttp", String.format("Received response for %s in %.1fms%n%s",
        response.request().url(), (t2 - t1) / 1e6d, response.headers()));

    return response;
  }
}

并添加它:

OkHttpClient client = new OkHttpClient.Builder()
  .addInterceptor(new LoggingInterceptor())
  .build();
于 2015-06-03T16:20:18.813 回答
17

拦截器功能目前正在审查中,但您可以通过在拉取请求中应用代码更改来使用该功能构建您自己的 okHttp 版本。

你可以用这样的东西实现你想要的功能

// Create an interceptor which catches requests and logs the info you want
RequestInterceptor logRequests= new RequestInterceptor() {
  public Request execute(Request request) {
    Log.i("REQUEST INFO", request.toString());
    return request; // return the request unaltered
  }
};

OkHttpClient client = new OkHttpClient();
List<RequestInterceptor> requestInterceptors = client.requestInterceptors();
requestInterceptros.add(logRequests);

如果您想查看更多内容,则拉取请求中包含一个测试。

我将不得不提前警告你使用它。拦截器 API 可能会有所更改,因为它尚未被合并。不要将它与生产代码一起使用,但它对于个人测试来说足够无害。

于 2014-08-23T08:35:56.917 回答
7

还没有。但是有一个正在开发的拦截器功能应该可以让它变得简单。

于 2014-07-25T12:19:01.983 回答
5

Square(员工)现在有一个官方解决方案。你可以试试: https ://github.com/square/okhttp/tree/master/okhttp-logging-interceptor

于 2017-04-22T15:29:17.500 回答
3

我添加了一些关于 OkHttp3 的信息,因为它支持下架。

首先,确保拥有这两个依赖项,即 OkHttp3 主包和包含记录器实现的特定包。我在这里使用3.14.6.

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.14.6</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>logging-interceptor</artifactId>
    <version>3.14.6</version>
</dependency>

然后正确设置您的 OkHttp 客户端。

...
import okhttp3.logging.HttpLoggingInterceptor;
...
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> logger.info(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .addNetworkInterceptor(interceptor)
        .build();

主要思想是您必须解释HttpLoggingInterceptor如何记录,因此在上面的示例中,消息只是路由到级别的Slf4j记录器INFO

于 2020-06-05T11:19:03.510 回答
3

您可以启用日志记录并使用 Timber 进行整合以仅在调试中登录。

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
      @Override
      public void log(String message) {
        Timber.tag("OkHttp: ");
        Timber.i(message);
      }
    }).setLevel(HttpLoggingInterceptor.Level.BODY);

   client = new OkHttpClient.Builder()
      .addInterceptor(httpLoggingInterceptor)
      .build();
于 2018-12-13T22:42:45.063 回答
3

对于 okhttp3

HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> Log.d(YourClass.class.getSimpleName(), "OkHttp: " + message));
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient.getHttpClient().interceptors().add(logging);
于 2020-03-13T13:32:00.990 回答
1

为了更好的用户界面和 OkHttp 网络调用的调试,您可以使用像GANDER这样的库

其他功能包括:

  1. 使用 Gander 的应用程序将显示一个通知,其中显示正在进行的 HTTP 活动的摘要。点击通知会启动完整的 Gander UI。应用程序可以选择性地抑制通知,并直接从它们自己的界面中启动 Gander UI。HTTP 交互及其内容可以通过共享意图导出。

  2. 搜索 HTTP 活动以及请求和响应

  3. 主要的 Gander 活动在其自己的任务中启动,允许它与使用 Android 7.x 多窗口支持的主机应用程序 UI 一起显示。
  4. Gander 提供以下变体
    • 持久性:将日志保存到磁盘并且可以控制 TTL
    • 在内存数据库中:只要应用程序生命周期,日志就会在内存中。
    • 无操作:这什么都不做。因此,如果用户只希望 Gander 在调试版本中使用,他们可以在不处理变体的情况下发布编译 NoOp,if(Build.DEBUG) ..etc
于 2019-06-08T13:12:28.593 回答
0

您可以使用以下方法添加日志:

   public final OkHttpClient client = new OkHttpClient.Builder()
       .addInterceptor(new HttpLoggingInterceptor())
       .cache(new Cache(cacheDir, cacheSize))
       .build();

哪个将使用默认配置:

Logger DEFAULT = message -> Platform.get().log(INFO, message, null);

于 2021-10-14T09:31:00.003 回答