What is the most convenient way to use SLF4J or other logging approaches with kotlin?
Usually the developer is busy with boilerplate code like
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
in each and every class to get a proper logger?
What are the most convenient ways to unify/simplify this with Kotlin?
Here's a simple example which returns a lazily-initialized logger from a bound callable reference or a standard property. I prefer calling from a callable reference because the
::
denotes reflection (related to logging).The class which provides the
Lazy<Logger>
:Inline functions to call them:
Here's an example of using them. The
require
assertion in the initializer shows that the loggers share a reference: