在Java中,我会写
logger.getLogger(NameOfClass.class)
。在Scala中该如何编写?logger.getLogger(NameOfClass.class)
。在Scala中该如何编写?你可以尝试:
logger.getLogger(getClass())
或者:
logger.getLogger(classOf[NameOfClass])
Logging
特质即可:class MyClass extends Logging {
logger.debug("This is very convenient ;-)")
}
object MyClass {
// Commons logging
private val clLogger = LogFactory.getLog(getClass())
// java.util.logging
private val juLogger = Logger.getLogger(getClass().toString)
}
class MyClass {
import MyClass._
...
}
class
上,它就是一个实例字段,每当该类的对象的new
实例时都会返回到日志记录器工厂。对于某些类来说,这可能是不必要或无法接受的性能问题。class MyClass {
// Commons logging
private static final Log clLogger = LogFactory.getLog(MyClass.class)
// java.util.logging
private static final Logger juLogger = Logger.getLogger(MyClass.class.toString)
...
}