Play framework 2.6:使用模块

时间:2018-06-19 09:27:28

标签: java playframework

我使用Play框架和自定义记录器(Log4j2)替换默认的Play记录器(Logback)。

我按照文档中描述的步骤操作: https://www.playframework.com/documentation/2.6.x/SettingsLogger

在build.sbt:

lazy val root = (project in file("."))
  .enablePlugins(PlayJava)
  .disablePlugins(PlayLogback)
libraryDependencies ++= Seq(
  "org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.4.1",
  "org.apache.logging.log4j" % "log4j-api" % "2.4.1",
  "org.apache.logging.log4j" % "log4j-core" % "2.4.1"
)

一切正常,直到我尝试添加为Play编写的自定义模块:https://github.com/alexanderjarvis/play-jongo

作为Play模块,它还有一个build.sbt,其中Logback不会被禁用。

尝试使用导入的模块运行我的软件时,出现此错误:

java.lang.ClassCastException: org.apache.logging.slf4j.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext
    at play.api.libs.logback.LogbackLoggerConfigurator.configure(LogbackLoggerConfigurator.scala:92)
    at play.api.libs.logback.LogbackLoggerConfigurator.init(LogbackLoggerConfigurator.scala:31)
    at play.core.server.DevServerStart$.$anonfun$mainDev$1(DevServerStart.scala:88)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.core.server.DevServerStart$.mainDev(DevServerStart.scala:59)
    at play.core.server.DevServerStart$.mainDevHttpMode(DevServerStart.scala:49)
    at play.core.server.DevServerStart.mainDevHttpMode(DevServerStart.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at play.runsupport.Reloader$.startDevMode(Reloader.scala:230)
    at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3.devModeServer$lzycompute$1(PlayRun.scala:79)
    at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3.play$sbt$run$PlayRun$$anonfun$$anonfun$$anonfun$$devModeServer$1(PlayRun.scala:79)
    at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3.apply(PlayRun.scala:102)
    at play.sbt.run.PlayRun$$anonfun$playRunTask$1$$anonfun$apply$2$$anonfun$apply$3.apply(PlayRun.scala:65)
    at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) java.lang.reflect.InvocationTargetException
[error] Total time: 1 s, completed 18 juin 2018 16:04:02

显然,模块的Logback传递依赖与导入的log4j2之间存在冲突。

因此我尝试在导入jongo模块时排除Logback:

"uk.co.panaxiom" %% "play-jongo" % "2.1.0-jongo1.3" exclude("ch.qos.logback", "logback-classic")

然后得到了一个完全不同的错误:

[WARN] [06/18/2018 15:59:12.432] [pool-6-thread-1] 
[EventStream(akka://play-dev-mode)] Logger log1-Slf4jLogger did not respond within Timeout(5000 milliseconds) to     InitializeLogger(bus)
error while starting up loggers
akka.ConfigurationException: Logger specified in config can't be loaded 
[akka.event.slf4j.Slf4jLogger] due to 
[akka.event.Logging$LoggerInitializationException: Logger log1-Slf4jLogger did not respond with LoggerInitialized, sent instead [TIMEOUT]]

使用自定义记录器导入Play模块时有没有人遇到过同样的问题?或者你对如何解决这个问题有任何想法吗?

感谢任何(有用的)建议:)

1 个答案:

答案 0 :(得分:0)

PlayLogback插件导致将"com.typesafe.play" %% "play-logback" % Play-Version模块添加到libraryDependencies。实际上play-jongo具有以下依赖性:http://www.mvnrepository.com/artifact/uk.co.panaxiom/play-jongo_2.12/2.1.0-jongo1.3

您需要从依赖项中排除此组件,以防止Play的LogbackLoggerConfigurator试图启动。

编辑

要排除Scala依赖关系,您必须在其名称中包含scala二进制版本:

"uk.co.panaxiom" %% "play-jongo" % "2.1.0-jongo1.3" exclude("com.typesafe.play", "play-logback_2.12")