
Works with logging is vital on developer although them we can track events, errors, and general application behavior.
This post let’s create ours logging in an easy way. With just a few files.
But this logging not works in the native code iOS, for native code iOS using println
-Project/ComposeApp/commonMain
expect object KmpLogger {
fun d(tag: String, message: String)
fun e(tag: String, message: String, throwable: Throwable? = null)
}-Project/ComposeApp/androidMain
actual object KmpLogger {
actual fun d(tag: String, message: String) {
android.util.Log.d(tag, message)
}
actual fun e(tag: String, message: String, throwable: Throwable?) {
android.util.Log.e(tag, message, throwable)
}
}-Project/ComposeApp/iosMain
actual object KmpLogger {
actual fun d(tag: String, message: String) {
println("DEBUG: [$tag] $message")
}
actual fun e(tag: String, message: String, throwable: Throwable?) {
val stackTrace = throwable?.stackTraceToString()
println("ERROR: [$tag] $message [$stackTrace]")
}
}How to use logging
@Composable
fun YourScreen(){
DisposableEffect(Unit) {
onDispose {
KmpLogger.d(tag = "tag", message = "message")
KmpLogger.e(tag = "tag", message = "message", throwable = Exception("my exception"))
}
}
}Obviously you can put your logging wherever you want.
Extras
You can access also ours GitHub with all code and news features;
If you have doubts, question don’t hesitate to ask; Will be a pleasure to help.
