Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update KtorSimpleLoggerJs.kt #4002

Merged
merged 1 commit into from Mar 21, 2024

Conversation

aggarwalpulkit596
Copy link
Contributor

@aggarwalpulkit596 aggarwalpulkit596 commented Mar 19, 2024

Subsystem
Ktor-Utils

Motivation
Configuring JS Client for Browser platform.

In our organisation we are currently using a Kotlin multiplatform based SDK which relies on ktor for networking. However there is a slight inconvience with the browser platform being bombarded with trace logs. When we were exploring possible solutions for the same, we found this https://github.com/ktorio/ktor/pull/3783/files, which solves our problem partially by adding a env for configuring trace logs but this doesn't work for us as our platform is a pure client side facing application which runs in browser.

Solution
Adding a simple conditional for the KtorSimpleLogger in which we check for both Node and Browser platform.

@e5l
Copy link
Member

e5l commented Mar 21, 2024

Hey @aggarwalpulkit596, thanks for the PR. LGTM, merged

@e5l e5l merged commit ff7c004 into ktorio:main Mar 21, 2024
1 check passed
@Legion2
Copy link

Legion2 commented Apr 3, 2024

Hi I'm trying to use ktor client in my wasm app and facing an issue related to this change:

process is not defined
ReferenceError: process is not defined
    at io.ktor.util.logging.getKtorLogLevel (webpack-internal:///./kotlin/composeApp.uninstantiated.mjs:4091:68)
    at <de.voize.core:web>.io.ktor.util.logging.getKtorLogLevel__externalAdapter (http://localhost:8080/composeApp.wasm:wasm-function[20569]:0x4596d6)
    at <de.voize.core:web>.io.ktor.util.logging.<no name provided>.<init> (http://localhost:8080/composeApp.wasm:wasm-function[20570]:0x459776)
    at <de.voize.core:web>.io.ktor.util.logging.KtorSimpleLogger (http://localhost:8080/composeApp.wasm:wasm-function[20568]:0x4596cc)
    at <de.voize.core:web>.io.ktor.client.plugins.<init properties HttpRequestLifecycle.kt> (http://localhost:8080/composeApp.wasm:wasm-function[22345]:0x47a83e)
    at <de.voize.core:web>.io.ktor.client.plugins.<get-HttpRequestLifecycle> (http://localhost:8080/composeApp.wasm:wasm-function[22314]:0x479dc1)
    at <de.voize.core:web>.io.ktor.client.HttpClient.<init> (http://localhost:8080/composeApp.wasm:wasm-function[21759]:0x46dee1)
    at <de.voize.core:web>.io.ktor.client.HttpClient.<init> (http://localhost:8080/composeApp.wasm:wasm-function[21765]:0x46e0df)
    at <de.voize.core:web>.io.ktor.client.HttpClient (http://localhost:8080/composeApp.wasm:wasm-function[21782]:0x46e4f1)
    at <de.voize.core:web>.App$lambda.invoke (http://localhost:8080/composeApp.wasm:wasm-function[69102]:0x8a0c95)

private fun getKtorLogLevel(): String? = js("process.env.KTOR_LOG_LEVEL")
In browser environment the process property is not defined which causes the ReferenceError.

@e5l
Copy link
Member

e5l commented Apr 3, 2024

Hey, as workaround you can try defining it manually in index.html, please check ktor-samples for the complete wasm sample.

https://github.com/ktorio/ktor-samples/blob/main/ktor-client-wasm/composeApp/src/wasmJsMain/resources/index.html

e5l pushed a commit that referenced this pull request Apr 4, 2024
@Stexxe
Copy link
Contributor

Stexxe commented Apr 5, 2024

Also, the runCatching problem (the error shouldn't be thrown) has been fixed in Kotlin multiplatform of version 2.0.0-Beta5.

e5l pushed a commit that referenced this pull request Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants