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

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

问题描述

默认情况下,所有 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 在 play 论坛中对一个问题的回答(参见 这个线程):

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

记录器目前在测试模式下被禁用,因为它导致运行测试时巨大的永久代空间泄漏.但我们正在努力运行在分叉的 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 输出!框架规范2测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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