如何启用Okhttp' sockWebserver记录?

发布于 2025-02-10 22:46:16 字数 1471 浏览 1 评论 0 原文

我在Android Project中使用了MockWebserver 3.14.9的旧版本,在运行UI测试时,我可以在LogCat中看到其日志。更新到最新的4.10.0之后,我看不到任何MockWeberver日志的请求和响应。看来,日志记录级别从,这没有更改。

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

我尝试调试它,并发现 if(logger.isloggable(level.fine))始终返回false。

Here's the code for Logger.isLoggable (

/**
* 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;
}

因此,通过在此方法中放置一个断点,我可以看到它返回false,因为请求的级别 fine> fine (( defined as 500

因此,上述PR是否将级别更改为 fine 不正确,因为它没有考虑到logger仍然是使用级别 info> info 更高的?

还是我缺少某些东西,有一种方法可以更改记录器级别或以其他方式查看MockWeberver的日志?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请别遗忘我 2025-02-17 22:46:16

您可以在此处粘贴从调试记录文档中粘贴配置:

OkHttpDebugLogging.enable(MockWebServer::class)

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

OkHttpDebugLogging.enable(MockWebServer::class)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文