【问题标题】:Why is Scalatra warning me about log4j为什么 Scalatra 警告我关于 log4j
【发布时间】:2025-10-18 06:22:08
【问题描述】:

我正在尝试让某种类型的日志记录在 Scalatra 中工作。我只是按照http://www.scalatra.org/2.2/guides/monitoring/logging.html 的说明进行操作。当我在使用 sbt 启动后运行 container:start 时,我在控制台中看到以下内容,多条看起来不完全正确的消息:

据我所知,我正在尝试使用 logback,但稍后会加载 log4j。这不是我所期望的行为,但这是我所看到的。

从多个绑定到 log4j 警告(我没有在这个应用程序的任何地方添加 log4j):

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/foo/.ivy2/cache/org.slf4j/slf4j-log4j12/jars/slf4j-log4j12-1.6.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/foo/.ivy2/cache/ch.qos.logback/logback-classic/jars/logback-classic-1.0.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
Null identity service, trying login service: null
Finding identity service: null
[info] started o.e.j.w.WebAppContext{/v1,[file:/Volumes/Macintosh%20HD%202/Dropbox/Projects/scalatra-test/src/main/webapp/]}
[info] started o.e.j.w.WebAppContext{/v1,[file:/Volumes/Macintosh%20HD%202/Dropbox/Projects/scalatra-test/src/main/webapp/]}
log4j:WARN No appenders could be found for logger (org.scalatra.servlet.ScalatraListener).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

不完全确定问题出在哪里。我只是希望能够:

  • 将一些调试语句记录到 STDOUT(假设它将进入我开始 sbt 的术语)
  • 将一些调试语句记录到我可以简单地尾随的文件中

我的依赖在 build.scala 中如下所示:

libraryDependencies ++= Seq(
    "org.scalatra" %% "scalatra" % ScalatraVersion,
    "org.scalatra" %% "scalatra-scalate" % ScalatraVersion,
    "org.scalatra" %% "scalatra-specs2" % ScalatraVersion % "test",
    "ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
    "org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container",
    "org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar")),
    "org.mongodb" %% "casbah" % "2.6.1",
    "org.scalatra" %% "scalatra-json" % "2.2.1",
    "org.json4s"   %% "json4s-native" % "3.2.4",
    //"org.json4s" %% "json4s-jackson" % "3.2.4",
    "org.scalatra" %% "scalatra-swagger"  % "2.2.1",
    "commons-codec" % "commons-codec" % "1.8"
  ),

这里是否有可能需要 log4j 并且覆盖 logback?

是否也可以指示 sbt 将 log4j 从该项目中完全排除?我找不到需要它作为依赖项的内容,但它应该使用 logback。

【问题讨论】:

    标签: scala logging log4j logback scalatra


    【解决方案1】:
    1. 你可以使用sbt-dependency-graph插件来检测依赖 在 log4j 上。
    2. 并将其从您的构建中排除。例如:

      libraryDependencies += "org.scalatra" %% "scalatra" % "2.2.1" exclude("org.slf4j", "log4j12")
      

      libraryDependencies += "org.scalatra" %% "scalatra" % "2.2.1" excludeAll(ExclusionRule(organization = "org.slf4j"))
      

    【讨论】:

    • 我相信答案中有错字,应该是 excludeAll(ExclusionRule(organization = "log4j"))。至少这个对我有用
    • 最初的想法是排除 log4j-slf4j 桥或所有传递 slf4j 依赖项并定义自己的。但在某些情况下,排除 log4j 可能就足够了。
    • 我最近在集成 scalatra-swagger 后遇到了同样的问题。由于我的应用程序使用了 logback,因此多个绑定问题就显现出来了。添加以下行: libraryDependencies += "org.scalatra" %% "scalatra-wagger" % "2.2.2" exclude("org.slf4j", "log4j12") 删除了问题。感谢您的提示,它为我节省了数小时的痛苦。