Question

I am attempting to get some type of logging working in Scalatra. I simply followed the instructions here http://www.scalatra.org/2.2/guides/monitoring/logging.html. When I run container:start after launching with sbt, I see the following in my console, multiple messages that don't exactly look right:

From what I can tell, I'm attempting to use logback, but something is loading log4j later on. That's not the behavior I'm expecting, but it is what I'm seeing.

From multiple bindings to log4j warnings (I didn't add log4j anywhere in this app):

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.

Not entirely sure what the problem is here. I just want to be able to:

  • Log some debug statements to STDOUT (assuming it would go to the term I started sbt from)
  • Log some debug statements to a file that I can simply tail

My dependencies look like the following in 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"
  ),

Is it possible something here is also requiring log4j and that's overriding logback?

Is it also possible to instruct sbt to completely exclude log4j from this project? I can't locate what's requiring it as a dependency, but it should be using logback instead.

Was it helpful?

Solution

  1. You can use sbt-dependency-graph plugin to detect what depends on log4j.
  2. And exclude it from your build. For example:

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

    or

    libraryDependencies += "org.scalatra" %% "scalatra" % "2.2.1" excludeAll(ExclusionRule(organization = "org.slf4j"))
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top