https://kotlinlang.org logo
#ktor
Title
# ktor
a

Arthur Krukowski

03/06/2023, 2:49 PM
Hi guys. How to disable browser console trace logs for the JS platform? Adding a custom logger doesn't help, it continues to spam with trace logs on each request. Here's a demo:
Copy code
private val myScope = CoroutineScope(CoroutineName("my-scope") + Job())
private val httpClient = newHttpClient(10_000)

class EmptyLogger : Logger {
    override fun log(message: String) {}
}

fun newHttpClient(timeoutMs: Long): HttpClient {
    return HttpClient {
        install(HttpTimeout) { requestTimeoutMillis = timeoutMs }
        install(Logging) {
            logger = EmptyLogger()
            level = LogLevel.NONE
        }
    }
}

@JsExport
fun testLogger() {
    myScope.launch {
        httpClient.get { url("<https://jsonplaceholder.typicode.com/todos/1>") }.bodyAsText()
    }
}
👍 1
@YentaBot How to disable trace logs for Ktor http client for JS platform?
👀 1
y

YentaBot

03/08/2023, 7:41 AM
It looks like you are trying to disable trace logs for Ktor http client for the JS platform. You can do this by creating a custom logger and setting the log level to NONE. Here is an example of how to do this:
Copy code
private val myScope = CoroutineScope(CoroutineName("my-scope") + Job()) private val httpClient = newHttpClient(10_000)  class EmptyLogger : Logger {     override fun log(message: String) {} }  fun newHttpClient(timeoutMs: Long): HttpClient {     return HttpClient {         install(HttpTimeout) { requestTimeoutMillis = timeoutMs }         install(Logging) {             logger = EmptyLogger()             level = LogLevel.NONE         }     } }  @JsExport fun testLogger() {     myScope.launch {         httpClient.get { url("<https://jsonplaceholder.typicode.com/todos/1>") }.bodyAsText()     } }
a

Arthur Krukowski

03/08/2023, 7:42 AM
ah, just pasted my demo above ... nice 😂
About this issue, seems it's a bug. And there's a related ticket on tracker: https://youtrack.jetbrains.com/issue/KTOR-5456/Ktor-JS-websocket-client-unconfigurable-logging
11 Views