跳到内容

调试日志

OkHttp 具有用于启用调试日志记录的内部 API。它使用 java.util.logging API,该 API 的配置可能很麻烦。作为一种快捷方式,您可以粘贴 OkHttpDebugLogging.kt。然后为您需要的任何功能启用调试日志记录

OkHttpDebugLogging.enableHttp2()
OkHttpDebugLogging.enableTaskRunner()

在 Android 上启用

$ adb shell setprop log.tag.okhttp.Http2 DEBUG
$ adb shell setprop log.tag.okhttp.TaskRunner DEBUG
$ adb logcat '*:E' 'okhttp.Http2:D' 'okhttp.TaskRunner:D'

HTTP/2 帧日志记录

这会记录 HTTP/2 连接的入站 (<<) 和出站 (>>) 帧。

[2020-01-01 00:00:00] >> CONNECTION 505249202a20485454502f322e300d0a0d0a534d0d0a0d0a
[2020-01-01 00:00:00] >> 0x00000000     6 SETTINGS
[2020-01-01 00:00:00] >> 0x00000000     4 WINDOW_UPDATE
[2020-01-01 00:00:00] >> 0x00000003    47 HEADERS       END_STREAM|END_HEADERS
[2020-01-01 00:00:00] << 0x00000000     6 SETTINGS
[2020-01-01 00:00:00] << 0x00000000     0 SETTINGS      ACK
[2020-01-01 00:00:00] << 0x00000000     4 WINDOW_UPDATE
[2020-01-01 00:00:00] >> 0x00000000     0 SETTINGS      ACK
[2020-01-01 00:00:00] << 0x00000003   322 HEADERS       END_HEADERS
[2020-01-01 00:00:00] << 0x00000003   288 DATA
[2020-01-01 00:00:00] << 0x00000003     0 DATA          END_STREAM
[2020-01-01 00:00:00] << 0x00000000     8 GOAWAY
[2020-01-01 00:00:05] << 0x00000000     8 GOAWAY

任务运行器日志记录

这会记录任务的排队、开始和完成。

[2020-01-01 00:00:00] Q10000 scheduled after   0 µs: OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10000 starting              : OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10000 run again after 300 s : OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10000 finished run in   1 ms: OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10001 scheduled after   0 µs: OkHttp squareup.com applyAndAckSettings
[2020-01-01 00:00:00] Q10001 starting              : OkHttp squareup.com applyAndAckSettings
[2020-01-01 00:00:00] Q10003 scheduled after   0 µs: OkHttp squareup.com onSettings
[2020-01-01 00:00:00] Q10003 starting              : OkHttp squareup.com onSettings
[2020-01-01 00:00:00] Q10001 finished run in   3 ms: OkHttp squareup.com applyAndAckSettings
[2020-01-01 00:00:00] Q10003 finished run in 528 µs: OkHttp squareup.com onSettings
[2020-01-01 00:00:00] Q10000 scheduled after   0 µs: OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10000 starting              : OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10000 run again after 300 s : OkHttp ConnectionPool
[2020-01-01 00:00:00] Q10000 finished run in 739 µs: OkHttp ConnectionPool