-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5671 from square/jwilson.1230.debug_logging
Document debug logging.
- Loading branch information
Showing
9 changed files
with
307 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
android-test/src/androidTest/java/okhttp/android/test/OkHttpDebugLogcat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2019 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okhttp.android.test | ||
|
||
import android.util.Log | ||
import okhttp3.internal.concurrent.TaskRunner | ||
import okhttp3.internal.http2.Http2 | ||
import java.util.concurrent.CopyOnWriteArraySet | ||
import java.util.logging.Handler | ||
import java.util.logging.Level | ||
import java.util.logging.LogRecord | ||
import java.util.logging.Logger | ||
import kotlin.reflect.KClass | ||
|
||
object OkHttpDebugLogcat { | ||
// Keep references to loggers to prevent their configuration from being GC'd. | ||
private val configuredLoggers = CopyOnWriteArraySet<Logger>() | ||
|
||
fun enableHttp2() = enable(Http2::class) | ||
|
||
fun enableTaskRunner() = enable(TaskRunner::class) | ||
|
||
private fun enable(loggerClass: KClass<*>) { | ||
val logger = Logger.getLogger(loggerClass.java.name) | ||
if (configuredLoggers.add(logger)) { | ||
logger.level = Level.FINE | ||
logger.addHandler(object : Handler() { | ||
override fun publish(record: LogRecord) { | ||
Log.i(loggerClass.simpleName, record.message, record.thrown) | ||
} | ||
|
||
override fun flush() { | ||
} | ||
|
||
override fun close() { | ||
} | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
Debug Logging | ||
============= | ||
|
||
OkHttp has internal APIs to enable debug logging. It uses the `java.util.logging` API which can be | ||
tricky to configure. As a shortcut, you can paste [OkHttpDebugLogging.kt]. Then enable debug logging | ||
for whichever features you need: | ||
|
||
``` | ||
OkHttpDebugLogging.enableHttp2() | ||
OkHttpDebugLogging.enableTaskRunner() | ||
``` | ||
|
||
On Android, use [OkHttpDebugLogcat.kt] instead. It connects OkHttp logs to Logcat in Android Studio. | ||
|
||
### HTTP/2 Frame Logging | ||
|
||
This logs inbound (`<<`) and outbound (`>>`) frames for HTTP/2 connections. | ||
|
||
``` | ||
[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 | ||
``` | ||
|
||
### Task Runner Logging | ||
|
||
This logs task enqueues, starts, and finishes. | ||
|
||
``` | ||
[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 | ||
``` | ||
|
||
[OkHttpDebugLogging.kt]: https://github.com/square/okhttp/blob/master/okhttp-testing-support/src/main/java/okhttp3/OkHttpDebugLogging.kt | ||
[OkHttpDebugLogcat.kt]: https://github.com/square/okhttp/blob/master/android-test/src/androidTest/java/okhttp/android/test/OkHttpDebugLogcat.kt |
49 changes: 49 additions & 0 deletions
49
okhttp-testing-support/src/main/java/okhttp3/OkHttpDebugLogging.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2019 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okhttp3 | ||
|
||
import okhttp3.internal.concurrent.TaskRunner | ||
import okhttp3.internal.http2.Http2 | ||
import java.util.concurrent.CopyOnWriteArraySet | ||
import java.util.logging.ConsoleHandler | ||
import java.util.logging.Level | ||
import java.util.logging.LogRecord | ||
import java.util.logging.Logger | ||
import java.util.logging.SimpleFormatter | ||
import kotlin.reflect.KClass | ||
|
||
object OkHttpDebugLogging { | ||
// Keep references to loggers to prevent their configuration from being GC'd. | ||
private val configuredLoggers = CopyOnWriteArraySet<Logger>() | ||
|
||
fun enableHttp2() = enable(Http2::class) | ||
|
||
fun enableTaskRunner() = enable(TaskRunner::class) | ||
|
||
private fun enable(loggerClass: KClass<*>) { | ||
val logger = Logger.getLogger(loggerClass.java.name) | ||
if (configuredLoggers.add(logger)) { | ||
logger.addHandler(ConsoleHandler().apply { | ||
level = Level.FINE | ||
formatter = object : SimpleFormatter() { | ||
override fun format(record: LogRecord) = | ||
String.format("[%1\$tF %1\$tT] %2\$s %n", record.millis, record.message) | ||
} | ||
}) | ||
logger.level = Level.FINE | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.