加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 09:46:55 所属栏目:安全 来源:网络整理
导读:默认情况下,当应用程序正在运行时,所有Logger输出(在应用程序运行时可见)在测试应用程序时静默。 如何强制调试,信息等在specs2报告中显示? 解决方法 首先,你可能喜欢在测试模式下禁用日志记录的一些背景。这是Guillame Bort在游戏论坛中对问题的回答(见
默认情况下,当应用程序正在运行时,所有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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读