scala – 如何强制Logger.debug输出在Play!框架specs2测试?

认情况下,当应用程序正在运行时,所有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"

相关文章

共收录Twitter的14款开源软件,第1页Twitter的Emoji表情 Tw...
Java和Scala中关于==的区别Java:==比较两个变量本身的值,即...
本篇内容主要讲解“Scala怎么使用”,感兴趣的朋友不妨来看看...
这篇文章主要介绍“Scala是一种什么语言”,在日常操作中,相...
这篇文章主要介绍“Scala Trait怎么使用”,在日常操作中,相...
这篇文章主要介绍“Scala类型检查与模式匹配怎么使用”,在日...