https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

Jeff

07/13/2020, 7:46 AM
Hello, My client doesn't print logs when I make requests through my Android app. Any help would be appreciated. I only get to see:
Copy code
I/System.out: [OkHttp] sendRequest>>
The code is as follows:
Copy code
private val client by lazy {
        HttpClient {
            install(JsonFeature) {
                serializer = KotlinxSerializer(
                    Json(
                        JsonConfiguration(
                            isLenient = true,
                            ignoreUnknownKeys = true
                        )
                    )
                )
            }
            install(Logging) {
                logger = Logger.DEFAULT
                level = LogLevel.ALL
            }
        }
    }
stackoverflow 1
s

saket

07/13/2020, 8:40 AM
I remember running into something similar. IIRC I put some debug breakpoints and found out that the current ktor logger was a no-op one.
j

Jeff

07/13/2020, 9:19 AM
I don't seem to get you clearly @saket are you saying that the Logger doesn't work? How did you fix that?
k

Kris Wong

07/13/2020, 1:41 PM
#ktor
r

russhwolf

07/13/2020, 1:59 PM
Ktor's default JVM logger depends on slf4j so you need to add a compatible logger dependency separately. You could do that via logback-android, for example. https://github.com/tony19/logback-android Easier however to define a custom logger like
Copy code
install(Logging) {
    logger = object : Logger {
        override fun log(message: String) {
            Log.v(TAG, message)
        }
    }
}
1
j

Jeff

07/13/2020, 2:02 PM
Thanks @russhwolf. The client code is on a shared module so I'm not sure how to go about it. It needs to work for other platforms too.
r

russhwolf

07/13/2020, 2:18 PM
One way is
expect val logger: Logger
and implement per-platform. You can also use
Logger.SIMPLE
instead of
Logger.DEFAULT
in common which just uses
println
on all platforms.
j

Jeff

07/13/2020, 2:32 PM
Final question: Whats the advantage of using a library like Kermit as opposed to my own implementation like so:
Copy code
### //JVM
actual fun logMessage(msg: String) {
    println(msg) ### //print the message to System.out
}

### // Android
actual fun logMessage(msg: String) {
    Log.d("AndroidApp", msg) ### //log the message to logcat
}

### // JS
actual fun logMessage(msg: String) {
    console.log(msg) // log the message to the browser console
}

### // iOS
actual fun logMessage(msg: Sring) {
    NSLog(msg) ### // log the message using NSLog
}
Does Kermit make the client logs more readable or something?
r

russhwolf

07/13/2020, 2:42 PM
Kermit does some of that work for you, and it's structured in a way that its configuration is safe to freeze for multithreaded native use if you're configuring multiple loggers. Doesn't do anything special in terms of format. You probably don't need it if all you're doing is those simple actuals.
j

Jeff

07/14/2020, 7:43 AM
Thanks