我创建了一个用于日志记录的扩展函数:
import org.slf4j.LoggerFactory
fun Any.log(msg: String) {
LoggerFactory.getLogger(javaClass.name).debug(msg)
}
但我不确定它是否会在任何时候被调用,因为方法LoggerFactory.getLogger
调用getILoggerFactory
。
Mb 有人已经做过类似的事情,可以向我保证不会有任何内存泄漏:)?
现在我使用老式方法(在类中声明记录器字段):
companion object {
private val logger = LoggerFactory.getLogger(LoggerTest::class.java.name)
}
但像这样的简单unit
测试:
@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")
}
显示与旧时尚选项相同的结果:
@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
我在用着:
org.slf4j - 1.7.25
ch.qos.logback - 1.2.3