I have create an extension function for logging:
import org.slf4j.LoggerFactory
fun Any.log(msg: String) {
LoggerFactory.getLogger(javaClass.name).debug(msg)
}
But i'm not sure about will it be init any time when it will be called or not, because method LoggerFactory.getLogger
invoking getILoggerFactory
.
Mb someone already did something like that and can assure me that it's won't be any memory leaks :) ?
For now i using old fashion approach(of declaring logger field in a class):
companion object {
private val logger = LoggerFactory.getLogger(LoggerTest::class.java.name)
}
but a simple unit
test like this:
@Test
fun testLogger() {
val start = System.currentTimeMillis()
for (i in 0..999) {
log("i=" + i)
}
val end = System.currentTimeMillis()
val time = end - start
println("*** TIME=" + time.toDouble() / 1000 + " sec")
}
shows same result as with old fashion option:
@Test
fun testLogger() {
val start = System.currentTimeMillis()
for (i in 0..999) {
logger.debug("i=" + i)
}
val end = System.currentTimeMillis()
val time = end - start
println("*** TIME=" + time.toDouble() / 1000 + " sec")
}
companion object {
private val logger = LoggerFactory.getLogger(LoggerTest::class.java.name)
}
~ *** TIME=0.02 sec
I'm using:
org.slf4j - 1.7.25
ch.qos.logback - 1.2.3