Okhttp: Disable logging for MockWebServer

Created on 16 Jun 2016  路  3Comments  路  Source: square/okhttp

Feature request: A way to disable the MockWebServer logging

I'm currently using MockWebServer for my tests, and it's spamming the test output with its log. As far as I can tell, there's no way to disable the logging of MockWebServer. This is an important missing feature for me since it's tough to find the information I want when the log is full of hundreds of lines like this:

INFO: MockWebServer[51602] received request: GET /api/v2/berry/34/ HTTP/1.1 and responded: HTTP/1.1 200 OK
Jun 15, 2016 9:20:52 PM okhttp3.mockwebserver.MockWebServer$4 processOneRequest

Most helpful comment

Here a concrete way to make MockWebServer a little less noisy ;)

Basically, you can use this anywhere
Logger.getLogger(MockWebServer::class.java.name).level = Level.WARNING

For example like this

import okhttp3.mockwebserver.MockWebServer
import java.util.logging.Level
import java.util.logging.Logger

/**
 * Avoids noise in the test output
 * by setting the logging level of [MockWebServer] to [Level.WARNING]
 * and then calling [MockWebServer.start]
 */
fun MockWebServer.startSilently() {
    Logger.getLogger(this::class.java.name).level = Level.WARNING
    start()
}

// That extension can be used like this
val mockWebServer = MockWebServer()
mockWebServer.startSilently()

All 3 comments

You can use the java.util.logging APIs to mute logging for any class, including MockWebServer.

Here a concrete way to make MockWebServer a little less noisy ;)

Basically, you can use this anywhere
Logger.getLogger(MockWebServer::class.java.name).level = Level.WARNING

For example like this

import okhttp3.mockwebserver.MockWebServer
import java.util.logging.Level
import java.util.logging.Logger

/**
 * Avoids noise in the test output
 * by setting the logging level of [MockWebServer] to [Level.WARNING]
 * and then calling [MockWebServer.start]
 */
fun MockWebServer.startSilently() {
    Logger.getLogger(this::class.java.name).level = Level.WARNING
    start()
}

// That extension can be used like this
val mockWebServer = MockWebServer()
mockWebServer.startSilently()

For those who want a copy-paste snippet for Java:

Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
// or totally silent
Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.OFF);
Was this page helpful?
0 / 5 - 0 ratings