okhttp: use FINE log for pure IOExceptions

This commit is contained in:
Jihun Cho 2019-10-24 14:06:25 -07:00
parent c166ec2c4e
commit 30f8f26f7a
2 changed files with 10 additions and 16 deletions

View File

@ -25,11 +25,7 @@ import io.grpc.okhttp.internal.framed.FrameWriter;
import io.grpc.okhttp.internal.framed.Header; import io.grpc.okhttp.internal.framed.Header;
import io.grpc.okhttp.internal.framed.Settings; import io.grpc.okhttp.internal.framed.Settings;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import okio.Buffer; import okio.Buffer;
@ -39,10 +35,6 @@ final class ExceptionHandlingFrameWriter implements FrameWriter {
private static final Logger log = Logger.getLogger(OkHttpClientTransport.class.getName()); private static final Logger log = Logger.getLogger(OkHttpClientTransport.class.getName());
// Some exceptions are not very useful and add too much noise to the log
private static final Set<String> QUIET_ERRORS =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("Socket closed")));
private final TransportExceptionHandler transportExceptionHandler; private final TransportExceptionHandler transportExceptionHandler;
private final FrameWriter frameWriter; private final FrameWriter frameWriter;
@ -231,9 +223,7 @@ final class ExceptionHandlingFrameWriter implements FrameWriter {
*/ */
@VisibleForTesting @VisibleForTesting
static Level getLogLevel(Throwable t) { static Level getLogLevel(Throwable t) {
if (t instanceof IOException if (t.getClass().equals(IOException.class)) {
&& t.getMessage() != null
&& QUIET_ERRORS.contains(t.getMessage())) {
return Level.FINE; return Level.FINE;
} }
return Level.INFO; return Level.INFO;

View File

@ -71,18 +71,22 @@ public class ExceptionHandlingFrameWriterTest {
} }
@Test @Test
public void quiet() { public void ioException() {
assertThat(getLogLevel(new IOException("Socket closed"))).isEqualTo(Level.FINE); assertThat(getLogLevel(new IOException("Socket closed"))).isEqualTo(Level.FINE);
} }
@Test @Test
public void nonquiet() { public void ioException_nullMessage() {
assertThat(getLogLevel(new IOException("foo"))).isEqualTo(Level.INFO); IOException e = new IOException();
assertThat(e.getMessage()).isNull();
assertThat(getLogLevel(e)).isEqualTo(Level.FINE);
} }
@Test @Test
public void nullMessage() { public void extendedIoException() {
IOException e = new IOException(); class ExtendedIoException extends IOException {}
ExtendedIoException e = new ExtendedIoException();
assertThat(e.getMessage()).isNull(); assertThat(e.getMessage()).isNull();
assertThat(getLogLevel(e)).isEqualTo(Level.INFO); assertThat(getLogLevel(e)).isEqualTo(Level.INFO);
} }