如何在Play中强制Logger.debug输出!框架specs2测试? [英] How to force Logger.debug output in Play! framework specs2 tests?

查看:64
本文介绍了如何在Play中强制Logger.debug输出!框架specs2测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,所有Logger输出(在应用程序运行时可见)在测试应用程序时处于静音状态.

By default all Logger output, visible when an application is running, is mute when the application is tested.

如何强制将调试,信息等显示在specs2报告中?

How to force the debugs, infos etc. to be shown in the specs2 reports?

推荐答案

首先,您可能会喜欢一些背景,为什么在测试模式下禁用了日志记录.这是Guillame Bort在游戏论坛中对一个问题的回答(请参阅

First off, you may like some background why logging is disabled in test mode. This was Guillame Bort's answer to a question in the play forum (see this thread):

该记录器暂时处于测试模式,因为它导致了 运行测试时,巨大的PermGen空间泄漏.但是我们正在努力运行 在分叉的JVM中进行测试,因此我们将很快再次启用它.

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代码):

As a workaround, I created my own logger like this (Scala code):

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泄漏,但是到目前为止,我还没有遇到这个问题. 要使其正常工作,您需要添加以下依赖项:

I don't know how this code behaves regarding the PermGen leak in general, but so far I didn't have that problem. To make it work you need to add this dependency:

"org.slf4j" % "slf4j-simple" % "1.6.4"

这篇关于如何在Play中强制Logger.debug输出!框架specs2测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆