1

I was using an old version of MockWebServer 3.14.9 in my Android project and I could see its logs in the logcat when running my UI Tests. After updating to the latest 4.10.0 I can't see any MockWebServer logs for requests and responses. It appears that the logging level was changed from INFO to FINE in this PR. As you can see in the latest code, that hasn't changed.

if (logger.isLoggable(Level.FINE)) {
    logger.fine(
      "${this@MockWebServer} received request: $request and responded: $response"
    )
  }

I tried debugging it and found that if (logger.isLoggable(Level.FINE)) always returns false.

Here's the code for Logger.isLoggable (source):

/**
* Check if a message of the given level would actually be logged
* by this logger.  This check is based on the Loggers effective level,
* which may be inherited from its parent.
*
* @param   level   a message logging level
* @return  true if the given message level is currently being logged.
*/
public boolean isLoggable(Level level) {
    int levelValue = config.levelValue;
    if (level.intValue() < levelValue || levelValue == offValue) {
        return false;
    }
    return true;
}

So by putting a breakpoint in this method I could see that it returns false because the requested level FINE (defined as 500 here), is apparently lower than the Logger's level INFO (defined as 800 here).

So was the aforementioned PR to change the level to FINE incorrect as it didn't consider that the Logger was still created with level INFO which is higher?

Or is there something I'm missing and there is a way to change the Logger level or otherwise see the logs from MockWebServer?

Just The Highlights
  • 1,555
  • 19
  • 31

1 Answers1

1

You can paste the configuration from the debug logging doc here: https://square.github.io/okhttp/contribute/debug_logging/

OkHttpDebugLogging.enable(MockWebServer::class)
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • Thanks for you answer. I tried that but I only see one log `MockWebServer[8080] starting to accept connections` at the Warn level and the tag `System.err`. I'm using MockWebServer in Android UI Tests by the way if that makes any difference (I've added that to my question). Also, would you happen to know if this approach is the intended way for adopters of OkHttp to enable logging or is this a workaround? – Just The Highlights Jun 25 '22 at 13:15
  • 2
    From that page you linked I tried the command `adb shell setprop log.tag.okhttp.MockWebServer DEBUG` and that results in seeing 2 logs `MockWebServer[8080] received request: GET /v5/launches HTTP/1.1 and responded: HTTP/1.1 200 OK` and `MockWebServer[8080] done accepting connections: Socket closed` but not the initial "starting to accept connections" log mentioned in my previous comment weirdly enough. Is there a way I can expand the logs in either case? I would like the logs to include the actual data sent and received like the headers & body. – Just The Highlights Jun 25 '22 at 13:50
  • 1
    If you want more logging, you might be able to accomplish something with a custom Dispatcher. But MockWebServer isn't really designed to have extensible logging in this way. – Jesse Wilson Jun 26 '22 at 15:02