I created 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 if this will be init anytime it is called or not, because the method LoggerFactory.getLoggercalls getILoggerFactory.
Mb someone has already done something similar and can assure me that he will not have memory leaks :)?
I am currently using the old fashion approach (declaring a registrar field in a class):
companion object {
private val logger = LoggerFactory.getLogger(LoggerTest::class.java.name)
}
but a simple test unitlooks 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 the same result as for the old fashion:
@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 use:
org.slf4j - 1.7.25
ch.qos.logback - 1.2.3
source
share