如何在Play framework v2.3测试中启用Logger?

时间:2015-03-03 09:24:14

标签: scala logging playframework-2.0 playframework-2.3

Play v2.3测试中的Logger无效:

"Test example" should {
  "Test 1" in {
    import play.api.Logger

    Logger.debug("hello world")  // doesn't work, no output on screen
  }
}

但是,如果我用WithApplication包装测试,它可以工作:

"Test example" should {
  "Test 1" in new WithApplication {
    import play.api.Logger

    Logger.debug("hello world")
  }
}

但如果我必须使用WithApplication包装每个测试只是为了让Logger工作,那么它会产生开销。

所以任何人都知道如何在没有WithApplication的情况下使Logger工作吗?

1 个答案:

答案 0 :(得分:0)

如果您正在使用scalatestplus,您应该覆盖记录器的FakeApplication附加配置,这里有一个正在运行的示例

import org.scalatest._
import play.api.test._
import play.api.test.Helpers._
import org.scalatestplus.play._

import play.api.Logger

class TestExampleLogger extends PlaySpec with OneAppPerSuite {

  // Override app if you need a FakeApplication with other than
  // default parameters.
  implicit override lazy val app: FakeApplication = FakeApplication(additionalConfiguration = Map(
    "logger.application" -> "DEBUG"
  ))

  "Test example" should {
    "Test 1" in {
      import play.api.Logger

      Logger.debug("hello world")  // doesn't work, no output on screen
    }
  }

}

希望有所帮助