question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document HttpLoggingInterceptor buffers content

See original GitHub issue

We should document (further) that the logging interceptor is not general purpose and fully buffers bodies.

Additionally, consider aggregating a single string for a request and a single string for each response to call the logger with to prevent interleaving on the console/logcat/wherever.

old issue below:


Hi, I’m working on an android app which downloads books. some of the books are really large and HttpLoggingInterceptor tries to log them and therefore I’m getting OutOfMemoryError which looks like this:

09-17 15:57:02.095 15408-15672/com.some.example E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
    Process: com.some.example, PID: 15408
    java.lang.OutOfMemoryError: Failed to allocate a 122708494 byte allocation with 16777216 free bytes and 93MB until OOM
        at java.lang.StringFactory.newStringFromBytes(StringFactory.java:79)
        at java.lang.StringFactory.newStringFromBytes(StringFactory.java:207)
        at okio.Buffer.readString(Buffer.java:631)
        at okio.Buffer.readString(Buffer.java:614)
        at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:273)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)

in production im not logging the body which fixes the error, but in development i want to be able to log it, how do you suggest me to tackle this issue? thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:21 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
swankjessecommented, Oct 23, 2018

Also – we don’t buffer the content ’cause we’re lazy and don’t know how to do streaming. We buffer it because we think that when a developer is viewing logs they don’t want the bodies of multiple responses to be interleaved. I know you don’t care about that, but that makes you special! And that’s why we have this interceptors API so you can do what you like.

And finally, it’s annoying to open source contributors when you threaten to take your business elsewhere. We’re not trying to build a library that meets everyone’s needs (because we think that would be big and complicated). Instead we’re trying to build a small but extensible library that meets most people’s needs. Whenever somebody says “add my feature or I’m leaving” it isn’t endearing.

3reactions
bpappincommented, Oct 23, 2018
  • yes used almost exclusivly in debugging, but should still never throw an OOM.
  • buffering may, or may not be important to the user. Generally the dev just needs to see what is going on, but it is reasonable to truncate, if you must buffer, in order to prevent an OOM.
  • How hard would it be to simply provide a non-buffering logger, or one that allowed you to specify what endpoints it used? I will contribute it myself, when I have some time, but someone managing the project would have to take ownership.
  • threatening to take my business elsewhere is not what this has been about, and I think you know that. It is about being able to trust the libraries you depend on for your livelihood, not to surprise you with something like an OOM.
  • I am flabbergasted that we are even having this discussion.
Read more comments on GitHub >

github_iconTop Results From Across the Web

okhttp-logging-interceptor/src/main/java/com/squareup/okhttp ...
Buffer ;. import okio.BufferedSource;. /**. * An OkHttp interceptor which logs request and response information. Can be applied as an.
Read more >
How to log request and response body with Retrofit-Android?
Add the following line to your gradle imports within your build.gradle file to fetch the logging interceptor dependency.
Read more >
Example usage for com.squareup.okhttp RequestBody writeTo
Document. Writes the content of this request to out . ... HttpLoggingInterceptor.java ... writeTo(buffer); Charset charset = UTF8; MediaType contentType ...
Read more >
okio.Buffer.clone java code examples - Tabnine
@Override public Source source(File file) throws FileNotFoundException { Buffer ... Buffer buffer = source.buffer(); //获取content的压缩类型 String encoding ...
Read more >
okhttp3.logging.HttpLoggingInterceptor Maven / Gradle / Ivy
HttpLoggingInterceptor maven / gradle build tool code. ... Show all versions of logging-interceptor Show documentation ... Buffer; import okio.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found