2017-01-02 50 views
1

我想在我的Play框架測試用例中禁止日誌記錄。解決這個問題的一種方法是在類路徑中使用slf4j-nop jar,而不是像logback-classic這樣的其他實現。我能夠在sbt中做到這一點,如下所示。Play Scala:在測試中使用Nop Logger來禁止登錄

libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.6" % "test" 

(dependencyClasspath in Test) <<= (dependencyClasspath in Test) map { 
    _.filterNot(_.data.name.contains("logback-classic")) 
} 

但我仍然得到下面的錯誤,當我運行我的測試

[info] Exception encountered when attempting to run a suite with class name: JobControllerSpec *** ABORTED *** 
[info] java.lang.NoClassDefFoundError: ch/qos/logback/classic/LoggerContext 
[info] at play.api.libs.logback.LogbackLoggerConfigurator.configure(LogbackLoggerConfigurator.scala:80) 
[info] at play.api.libs.logback.LogbackLoggerConfigurator.configure(LogbackLoggerConfigurator.scala:62) 
[info] at play.api.inject.guice.GuiceApplicationBuilder$$anonfun$applicationModule$1.apply(GuiceApplicationBuilder.scala:102) 
[info] at play.api.inject.guice.GuiceApplicationBuilder$$anonfun$applicationModule$1.apply(GuiceApplicationBuilder.scala:102) 
[info] at scala.Option.foreach(Option.scala:257) 
[info] at play.api.inject.guice.GuiceApplicationBuilder.applicationModule(GuiceApplicationBuilder.scala:101) 
[info] at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181) 
[info] at play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123) 
[info] at org.scalatestplus.play.OneAppPerSuite$class.app(OneAppPerSuite.scala:126) 
[info] at JobControllerSpec.app$lzycompute(JobControllerSpec.scala:12) 
[info] ... 
[info] Cause: java.lang.ClassNotFoundException: ch.qos.logback.classic.LoggerContext 
[info] at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 
[info] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
[info] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) 
[info] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
[info] at play.api.libs.logback.LogbackLoggerConfigurator.configure(LogbackLoggerConfigurator.scala:80) 
[info] at play.api.libs.logback.LogbackLoggerConfigurator.configure(LogbackLoggerConfigurator.scala:62) 
[info] at play.api.inject.guice.GuiceApplicationBuilder$$anonfun$applicationModule$1.apply(GuiceApplicationBuilder.scala:102) 
[info] at play.api.inject.guice.GuiceApplicationBuilder$$anonfun$applicationModule$1.apply(GuiceApplicationBuilder.scala:102) 
[info] at scala.Option.foreach(Option.scala:257) 
[info] at play.api.inject.guice.GuiceApplicationBuilder.applicationModule(GuiceApplicationBuilder.scala:101) 

爲什麼會有上的logback經典的LogbackLoggerConfigurator.scala硬依賴?

這是我的依賴狀態

[project] $ show libraryDependencies 
[info] List(org.scala-lang:scala-library:2.11.8, com.typesafe.play:twirl-api:1.1.1, com.typesafe.play:play-server:2.5.4, com.typesafe.play:play-test:2.5.4:test, com.typesafe.play:play-omnidoc:2.5.4:docs, com.typesafe.play:play-netty-server:2.5.4, com.typesafe.play:play-logback:2.5.4, com.typesafe.play:play-cache:2.5.4, com.typesafe.play:play-ws:2.5.4, com.typesafe.play:play-slick:2.0.0, com.typesafe.play:play-slick-evolutions:2.0.0, com.h2database:h2:1.4.187, mysql:mysql-connector-java:5.1.6, net.codingwell:scala-guice:4.1.0, org.scalatestplus.play:scalatestplus-play:1.5.0:test, org.slf4j:slf4j-nop:1.7.6:test) 

回答

0

我有slf4j-nop沒有經驗,但你可以提供test/resources/logback.xml爲測試一種特殊的logback的配置,將覆蓋在conf/logback.xml

+0

一個呀這就是我如何解決了這個問題,但試圖理解爲什麼上述方法沒有,以及如何引入logback-classic的編譯時間依賴性 –