scala - 如何在 Play 中强制输出 Logger.debug!框架规范2测试?

标签 scala playframework playframework-2.0 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"

关于scala - 如何在 Play 中强制输出 Logger.debug!框架规范2测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12334769/

相关文章:

scala - 解析包含代码的字符串,返回一个 Type

java - Play Framework - 表单的多个实例

scala - 玩 scala - 通过 Guice Injector 实例注入(inject)依赖项

java - 使用play作为代理服务器时如何重写绝对路径?

scala - 伴生对象中的隐式 val

java - SSE 与 angularJS 和 java in play 框架

java - Play Framework 2.x : How do i make a HTTP-GET for an reverse lookup (nominatim OSM)?

java - 以编程方式向 yarn 提交 spark 应用程序

scala - ALS Spark Scala 中的检查点

scala - Play Framework : Modules were resolved with conflicting cross-version suffixes