Idiomatic way of logging in Kotlin
Hello everyone! I'm new to Kotlin and transitioning from Java. In Java, I used to use SLF4J with Logback and declare a logger like this:
```java
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
```
In Kotlin, I see that many people use something like `companion object` for the logger. But I'm not sure if that's idiomatic. Plus, I came across the `kotlin-logging` library, which seems to simplify things.
My current code looks like this:
```kotlin
class MyService {
companion object {
private val logger = LoggerFactory.getLogger(MyService::class.java)
}
fun doSomething() {
logger.info("Doing something")
}
}
```
This works, but it feels cumbersome. Do I need to write `companion object` every time? Is there a shorter and cleaner way?
No answers yet.
Log in to leave an answer
Blogs