Hello, My client doesn't print logs when I make re...
# multiplatform
j
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
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
I don't seem to get you clearly @saket are you saying that the Logger doesn't work? How did you fix that?
k
#ktor
r
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
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
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
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
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
Thanks