the request code
var myClient: HttpClient = HttpClient(Android) {
// Logging
install(Logging) {
logger = Logger.ANDROID
level = LogLevel.BODY
}
}
when try to request URL
myClient.get("https://www.sample.com/state")
try to run the request and got the following request log
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
response log
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
In the log, show the request URL https://www.sample.com/state
twice.
For security reasons, we don't want to display this URL in the log.
How do I set or operate not to display this URL?
the kotlin version and ktor version
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"
You can either write your own interceptors and log only desired information (you can use the source code of the Logging plugin as an example) or write a logger that will remove unwanted information from messages. The latter solution is naive and fragile:
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")
}