【问题标题】:Ktor client: how to remove(not include) REQUEST/FROM logKtor 客户端:如何删除(不包括)REQUEST/FROM 日志
【发布时间】:2022-07-05 17:53:54
【问题描述】:

请求代码

var myClient: HttpClient = HttpClient(Android) {
    // Logging
    install(Logging) {
        logger = Logger.ANDROID
        level = LogLevel.BODY
    }
}

尝试请求 URL 时

myClient.get(\"https://www.sample.com/state\")

尝试运行请求并得到以下请求日志

2022-07-05 11:20:58.667 977-1021/? W/System.err: [DefaultDispatcher-worker-1] INFO io.ktor.client.HttpClient - REQUEST: https://www.sample.com/state
2022-07-05 11:20:58.667 977-1021/? W/System.err: METHOD: HttpMethod(value=GET)
2022-07-05 11:20:58.667 977-1021/? W/System.err: BODY Content-Type: null
2022-07-05 11:20:58.667 977-1021/? W/System.err: BODY START
2022-07-05 11:20:58.667 977-1021/? W/System.err: 
2022-07-05 11:20:58.667 977-1021/? W/System.err: BODY END

响应日志


2022-07-05 11:20:58.924 977-2181/? W/System.err: [DefaultDispatcher-worker-2] INFO io.ktor.client.HttpClient - RESPONSE: 200 OK
2022-07-05 11:20:58.924 977-2181/? W/System.err: METHOD: HttpMethod(value=GET)
2022-07-05 11:20:58.924 977-2181/? W/System.err: FROM: https://www.sample.com/state
2022-07-05 11:20:58.924 977-2181/? W/System.err: BODY Content-Type: application/json; charset=utf-8
2022-07-05 11:20:58.924 977-2181/? W/System.err: BODY START
2022-07-05 11:20:58.924 977-2181/? W/System.err: \"idle\"
2022-07-05 11:20:58.924 977-2181/? W/System.err: BODY END

在日志中,显示请求 URL https://www.sample.com/state 两次。

出于安全原因,我们不想在日志中显示此 URL。

如何设置或操作不显示此网址?

kotlin 版本和 ktor 版本


def kotlin_version = \'1.6.21\'
implementation \"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version\"

def kotlinx_coroutines_version = \'1.6.1\'
implementation \"org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines_version\"
implementation \"org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version\"

def ktor_version = \'2.0.2\'
implementation \"io.ktor:ktor-client-core:$ktor_version\"
// HTTP engine: The HTTP client used to perform network requests.
implementation \"io.ktor:ktor-client-android:$ktor_version\"
// Logging
implementation \"io.ktor:ktor-client-logging:$ktor_version\"
  • 删除 LogLevel.BODY。通过删除它,您的请求将不会显示在日志中。
  • 但除了 URL,所有内容都是必需的 @pratik vekariya
  • 我的意思是你不需要添加级别。我用java做过,如果你想看我可以给你看。
  • 尝试删除LogLevel.BODY,日志级别更改为默认(ALL),日志内容一切。
  • i.stack.imgur.com/zZWxY.png 检查此屏幕截图,在此我仅添加了 networkConnectionInterceptor。而且它没有在日志中显示请求

标签: android kotlin ktor ktor-client


【解决方案1】:

您可以编写自己的拦截器并仅记录所需的信息(您可以使用source code of the Logging plugin 作为示例)或编写一个记录器来从消息中删除不需要的信息。后一种解决方案是幼稚且脆弱的:

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import org.slf4j.LoggerFactory

suspend fun main() {
    val myLogger = object : Logger {
        private val urlRegex = Regex("""FROM: .+?$""", RegexOption.MULTILINE)
        private val delegate = LoggerFactory.getLogger(HttpClient::class.java)!!
        override fun log(message: String) {
            val strippedMessage = urlRegex.replace(message, "FROM: ***")
            delegate.info(strippedMessage)
        }
    }

    val client = HttpClient(OkHttp) {
        install(Logging) {
            logger = myLogger
            level = LogLevel.ALL
        }
    }

    client.get("https://httpbin.org/get")
}

【讨论】:

  • 非常感谢。我会根据这个想法来定制我的logger界面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-24
  • 2018-11-22
  • 2021-06-04
  • 2021-07-07
  • 2021-12-08
  • 2023-04-02
相关资源
最近更新 更多