From the course: Exploring Ktor with Kotlin Multiplatform and Compose
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Solution: Platform-specific logging - Kotlin Tutorial
From the course: Exploring Ktor with Kotlin Multiplatform and Compose
Solution: Platform-specific logging
(bright upbeat music) - [Instructor] How'd you do? Were you able to get logging working in shared code? Hopefully, you found an approach that felt clean and makes sense to you. There are different ways you could solve this, but here's how I tackled it. I started by defining an expect class called PlatformLogger in shared code. Notice that it has the expect before it. Then, I created the platform-specific versions in jvmMain. Here, we're using println and System.err.println. Also in androidMain, here we used the Log.i and Log.e. And finally, iosMain, where we use NSLog to print it out. Once that was in place, I called the logger from my shared service code. Now, what I actually do is a little different. I said, let's put this into our actual MainActivity. So here we get a message that says, "Hey, MainActivity launched." Now, the same logging call works no matter what. So if I take a look inside right now, I do have the Android app running in the background. It's been running for a bit.…