@udalov: Logback has logger cache, so it’s not really that “logger will be created for each instance of the class”, and I would assume other implementations do too, so I see no harm in using non-static val for it.
There is also a thing to keep in mind when you subclass a class with a logger. If using javaClass instead of specifying class statically, log statements from a base class will have logger corresponding to child class which might be misleading, e.g.:
open class A {
val logger1 = LoggerFactory.getLogger(javaClass)
val logger2 = LoggerFactory.getLogger(A::class.java)
fun sayHello() {
logger1.info("hello from logger 1")
logger2.info("hello from logger 2")
}
}
class B : A()
fun main(args: Array<String>) {
B().sayHello()
}
will output
[main] INFO B - hello from logger 1
[main] INFO A - hello from logger 2