Skip to content

HttpLoggingInterceptor logging at level.BODY does not allow streaming requests #3157

@foxish

Description

@foxish

What kind of issue is this?

  • Question. This issue tracker is not the place for questions. If you want to ask how to do
    something, or to understand why something isn't working the way you expect it to, use Stack
    Overflow. https://stackoverflow.com/questions/tagged/okhttp

  • Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests
    get fixed. Here’s an example: https://gist.github.com/swankjesse/981fcae102f513eb13ed

  • Feature Request. Start by telling us what problem you’re trying to solve. Often a solution
    already exists! Don’t send pull requests to implement new features without first getting our
    support. Sometimes we leave features out on purpose to keep the project small.

package blah;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import okhttp3.logging.HttpLoggingInterceptor.Level;
import okio.BufferedSource;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class t {
  public static void main(String[] args) throws IOException {
    HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
    logger.setLevel(Level.BODY);

    OkHttpClient client = new OkHttpClient().newBuilder().readTimeout(0, TimeUnit.MILLISECONDS)
        .addNetworkInterceptor(logger)
        .build();
    URL url = null;
    try {
      url = new URL("http://server:port/path");
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    HttpUrl.Builder httpUrlBuilder = HttpUrl.get(url).newBuilder();
    httpUrlBuilder
        .addQueryParameter("resourceVersion", "3266924")
        .addQueryParameter("watch", "true");

    Request request = new Request.Builder()
        .get()
        .url(httpUrlBuilder.build())
        .build();

    client.newCall(request).enqueue(new Callback() {
      public void onFailure(Call call, IOException e) {
        System.out.println("Fail");
      }

      public void onResponse(Call call, Response response) throws IOException {
        BufferedSource source = response.body().source();
        while (!source.exhausted()) {
          String line = source.readUtf8LineStrict();
          System.out.println(line);
        }
      }
    });
  }
}

I cannot stream responses if I set logger.setLevel(Level.BODY);, and it stops streaming any responses and gets stuck with:

Feb 07, 2017 2:38:53 PM okhttp3.internal.platform.Platform log
INFO: Content-Type: application/json
Feb 07, 2017 2:38:53 PM okhttp3.internal.platform.Platform log
INFO: Date: Tue, 07 Feb 2017 22:38:53 GMT
Feb 07, 2017 2:38:53 PM okhttp3.internal.platform.Platform log
INFO: Transfer-Encoding: chunked

However, if I set it to Level.BASIC, it works as expected. I think behaviorally, I wouldn't expect a logging interceptor to change how the actual response behaves.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions