1
0
mirror of https://github.com/square/okhttp.git synced 2025-08-07 12:42:57 +03:00

Document debug logging.

Also refine the formatting of TaskRunner logging.

Before:

    [2019-12-30 16:43:19] Q1 starting: OkHttp ConnectionPool
    [2019-12-30 16:43:19] Q1 scheduled after 300 s: OkHttp ConnectionPool
    [2019-12-30 16:43:19] Q1 finished in 94 µs: OkHttp ConnectionPool
    [2019-12-30 16:43:19] Q359 scheduled after 0 µs: OkHttp www.youtube.com applyAndAckSettings
    [2019-12-30 16:43:19] Q359 starting: OkHttp www.youtube.com applyAndAckSettings
    [2019-12-30 16:43:19] Q361 schedule canceled (queue is shutdown): OkHttp www.youtube.com onSettings
    [2019-12-30 16:43:19] Q359 finished in 382 µs: OkHttp www.youtube.com applyAndAckSettings
    [2019-12-30 16:43:20] Q1 scheduled after 0 µs: OkHttp ConnectionPool
    [2019-12-30 16:43:20] Q365 scheduled after 0 µs: OkHttp android-developers.googleblog.com applyAndAckSettings
    [2019-12-30 16:43:20] Q1 starting: OkHttp ConnectionPool
    [2019-12-30 16:43:20] Q365 starting: OkHttp android-developers.googleblog.com applyAndAckSettings
    [2019-12-30 16:43:20] Q1 scheduled after 300 s: OkHttp ConnectionPool
    [2019-12-30 16:43:20] Q1 finished in 151 µs: OkHttp ConnectionPool

After:

    [2019-12-30 16:43:19] Q10001 starting              : OkHttp ConnectionPool
    [2019-12-30 16:43:19] Q10001 run again after 300 s : OkHttp ConnectionPool
    [2019-12-30 16:43:19] Q10001 finished run in  94 µs: OkHttp ConnectionPool
    [2019-12-30 16:43:19] Q10359 scheduled after   0 µs: OkHttp www.youtube.com applyAndAckSettings
    [2019-12-30 16:43:19] Q10359 starting              : OkHttp www.youtube.com applyAndAckSettings
    [2019-12-30 16:43:19] Q10361 schedule canceled (queue is shutdown): OkHttp www.youtube.com onSettings
    [2019-12-30 16:43:19] Q10359 finished run in 382 µs: OkHttp www.youtube.com applyAndAckSettings
    [2019-12-30 16:43:20] Q10001 scheduled after   0 µs: OkHttp ConnectionPool
    [2019-12-30 16:43:20] Q10365 scheduled after   0 µs: OkHttp android-developers.googleblog.com applyAndAckSettings
    [2019-12-30 16:43:20] Q10001 starting              : OkHttp ConnectionPool
    [2019-12-30 16:43:20] Q10365 starting              : OkHttp android-developers.googleblog.com applyAndAckSettings
    [2019-12-30 16:43:20] Q10001 run again after 300 s : OkHttp ConnectionPool
    [2019-12-30 16:43:20] Q10001 finished run in 151 µs: OkHttp ConnectionPool
This commit is contained in:
Jesse Wilson
2019-12-30 17:43:54 -05:00
parent b9e042284b
commit a815093721
9 changed files with 307 additions and 139 deletions

View File

@@ -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
}
}
}