默认情况下,所有 Logger
输出,在应用程序运行时可见,在测试应用程序时是静音的。
如何强制在 specs2
报告中显示调试、信息等?
最佳答案
首先,您可能会了解为什么在测试模式下禁用日志记录的一些背景知识。这是 Guillame Bort 对游戏论坛中一个问题的回答(参见 this thread ):
The logger is disabled in test mode for now because it was causing an huge PermGen space leak when running tests. But we are working to run tests in a forked JVM so we will enable it again soon.
作为解决方法,我创建了自己的记录器(Scala 代码):
import play.api.{Play, LoggerLike, Logger}
import org.slf4j.LoggerFactory
import org.slf4j.impl.SimpleLoggerFactory
object MyLogger extends LoggerLike {
val factory = if (Play.isTest(Play.current)) {
new SimpleLoggerFactory()
} else {
LoggerFactory.getILoggerFactory
}
val redirectDebugToInfo = factory.isInstanceOf[SimpleLoggerFactory]
val logger = factory.getLogger("application")
def apply(name: String): Logger = new Logger(factory.getLogger(name))
def apply[T](clazz: Class[T]): Logger = new Logger(factory.getLogger(clazz.getCanonicalName))
// this method is to make debug statements to show up in test mode
override def debug(m: => String) = {
if (redirectDebugToInfo) {
info(m)
} else {
super.debug(m)
}
}
}
我不知道这段代码在 PermGen 泄漏方面的一般表现如何,但到目前为止我没有遇到那个问题。 要使其工作,您需要添加此依赖项:
"org.slf4j" % "slf4j-simple" % "1.6.4"
关于scala - 如何在 Play 中强制输出 Logger.debug!框架规范2测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334769/