scalaplayframeworksbtslf4j

Play Framework SLF4J Error Wehn Starting Server


I have the following dependencies for my Play Framework project:

  "io.getquill" %% "quill-jdbc" % "4.3.0",
  "org.postgresql" %  "postgresql" % "42.3.1",
  "com.h2database" % "h2" % "1.4.199",

  "org.tpolecat" %% "doobie-core"     % DoobieVersion,
  "org.tpolecat" %% "doobie-postgres" % DoobieVersion,
  "org.tpolecat" %% "doobie-h2" % DoobieVersion,
  "org.tpolecat" %% "doobie-hikari"   % DoobieVersion,

  "com.typesafe.play" %% "play-json" % "2.9.3",
  "com.typesafe.play" %% "play-json-joda" % "2.9.3",

  // Threading & Streaming libraries (https://monix.io/docs/current/intro/usage.html)
  "io.monix" %% "monix-execution" % "3.4.0",
  //"io.monix" %% "monix-eval" % "3.4.0",
  //"io.monix" %% "monix-reactive" % "3.4.1",
  "io.monix" %% "monix-reactive" % "3.4.1" exclude("org.typelevel", "cats-effect_2.13"),
  "org.typelevel" %% "cats-effect" % "3.5.1",

  // Test libraries
  "org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test,
  "org.specs2" %% "specs2-core" % "4.20.0" % Test,
  "org.scalacheck" %% "scalacheck" % "1.14.1" % Test

When I tried to run my project, it fails with the following error:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to 1.8.
SLF4J: Ignoring binding found at [jar:file:/home/joesan/.cache/coursier/v1/https/repo1.maven.org/maven2/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.
[error] java.lang.ClassCastException: class org.slf4j.helpers.NOPLoggerFactory cannot be cast to class ch.qos.logback.classic.LoggerContext (org.slf4j.helpers.NOPLoggerFactory and ch.qos.logback.classic.LoggerContext are in unnamed module of loader play.runsupport.NamedURLClassLoader @b900ff7)
[error]         at play.api.libs.logback.LogbackLoggerConfigurator.configure(LogbackLoggerConfigurator.scala:91)
[error]         at play.api.libs.logback.LogbackLoggerConfigurator.init(LogbackLoggerConfigurator.scala:31)
[error]         at play.core.server.DevServerStart$.$anonfun$mainDev$1(DevServerStart.scala:106)
[error]         at play.utils.Threads$.withContextClassLoader(Threads.scala:22)
[error]         at play.core.server.DevServerStart$.mainDev(DevServerStart.scala:76)
[error]         at play.core.server.DevServerStart$.mainDevHttpMode(DevServerStart.scala:50)
[error]         at play.core.server.DevServerStart.mainDevHttpMode(DevServerStart.scala)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[error]         at play.runsupport.Reloader$.startDevMode(Reloader.scala:306)
[error]         at play.sbt.run.PlayRun$.devModeServer$lzycompute$1(PlayRun.scala:100)
[error]         at play.sbt.run.PlayRun$.devModeServer$1(PlayRun.scala:83)
[error]         at play.sbt.run.PlayRun$.$anonfun$playRunTask$3(PlayRun.scala:107)
[error]         at play.sbt.run.PlayRun$.$anonfun$playRunTask$3$adapted(PlayRun.scala:67)
[error]         at scala.Function1.$anonfun$compose$1(Function1.scala:49)
[error] stack trace is suppressed; run last Compile / run for the full output
[error] (Compile / run) java.lang.reflect.InvocationTargetException
[error] Total time: 1 s, completed Sep 6, 2023, 9:33:00 PM

I know that there is some offending transitive dependency somewhere, but not sure how to identify and get it fixed.

EDIT: Running sbt evicted gives me this:

[warn]  * org.slf4j:slf4j-api:2.0.7 is selected over {1.7.30, 1.7.36, 1.7.36, 1.7.36, 1.7.30, 2.0.0-alpha1, 1.7.25, 1.7.36, 1.7.36, 1.7.32}
[warn]      +- ch.qos.logback:logback-classic:1.4.11              (depends on 2.0.7)
[warn]      +- org.tpolecat:doobie-hikari_2.13:1.0.0-RC1          (depends on 1.7.32)
[warn]      +- org.slf4j:jul-to-slf4j:1.7.36                      (depends on 1.7.36)
[warn]      +- org.slf4j:jcl-over-slf4j:1.7.36                    (depends on 1.7.36)
[warn]      +- org.java-websocket:Java-WebSocket:1.4.0            (depends on 1.7.25)
[warn]      +- com.zaxxer:HikariCP:4.0.3                          (depends on 2.0.0-alpha1)
[warn]      +- com.typesafe.scala-logging:scala-logging_2.13:3.9.4 (depends on 1.7.30)
[warn]      +- com.typesafe.play:play_2.13:2.8.19                 (depends on 1.7.36)
[warn]      +- com.typesafe.akka:akka-slf4j_2.13:2.6.20           (depends on 1.7.36)
[warn]      +- com.typesafe.akka:akka-actor-typed_2.13:2.6.20     (depends on 1.7.36)
[warn]      +- com.openelectrons:ocpp-j-api_2.13:0.0.1-SNAPSHOT   (depends on 1.7.30)

Solution

  • As of writing these lines (August 2023), in a Play application (2.8.19), you will likely encounter conflicts between SLF4J, logback, and maybe even other logging libraries.

    The short answer is:

    • enforce a version of SLF4J is used: either 2.x or 1.7.x but not both
    • enforce a logback version compatible with the SLF4J one:
      • 1.2.x if SLF4J 1.x,
      • 1.4.x if SLF4J 2.x (I'm assuming you're not running Java 8 thus don't need 1.3.x)
        • but 1.4.8 max because if you're using Play < 2.8.21 (see this bug for context)
        • latest version is fine if you're using 2.8.21+ (including 2.9 and 3.0)

    To do that, use dependencyOverrides to enforce specific versions of SLF4J and logback are used. This should be at least 5 dependencies like slf4j-api, jul-to-slf4j, jcl-over-slf4j, logback-core, logback-classic... (I don't know them all but sbt dependencyBrowseTree or a similar command will help finding them)