我试图压制我的Play框架测试用例中的日志记录。实现这一目标的一种方法是在类路径中使用slf4j-nop jar而不是像logback-classic这样的其他实现。我能够在sbt中做到这一点,如下所示。
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)
为什么LogbackLoggerConfigurator.scala中存在对logback-classic的硬依赖?
这是我的依赖项的状态
[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 :(得分:0)
我没有使用slf4j-nop
的经验,但您可以为test/resources/logback.xml
中的测试提供特殊的回溯配置,该配置会覆盖conf/logback.xml