2

I've isolated this to a very simple test project that has no purpose other than simple log4j2test configuration + usage. The file directory structure:

.
├── build.sbt
└── src
└── main
    ├── resources
    │   └── log4j2.xml
    └── scala
    └── mycompany
        └── myapp
        └── SimpleMain.scala

Build.sbt:

name := """log4j2test"""

version := "1.0.0-SNAPSHOT"

scalaVersion := "2.12.1"

scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")

libraryDependencies ++= {
  Seq(
"org.apache.logging.log4j"         % "log4j-slf4j-impl" % "2.8",
"org.apache.logging.log4j"         % "log4j-api" % "2.8",
"org.apache.logging.log4j"         % "log4j-core" % "2.8",
"com.fasterxml.jackson.dataformat" % "jackson-dataformat-yaml" % "2.8.6"
  )
}

log4j2.xml contents are copy/pasted from example in official documentation: https://logging.apache.org/log4j/2.x/manual/configuration.html

SimpleMain.scala:

package mycompany.myapp

import org.slf4j.LoggerFactory

object SimpleMain {
  val logger = LoggerFactory.getLogger(getClass())

  def main(args: Array[String]): Unit = {
println("simple println test")
logger.info("this is a logger.info message")
logger.debug("this is a logger.debug message")
logger.error("this is a logger.error message")

val stream = this.getClass.getClassLoader.getResourceAsStream("log4j2.xml")
if (stream == null) {
  println("failed to open log4j2.xml on classpath")
} else {
  println("successfully opened log4j2.xml on classpath")
  stream.close()
}
  }
}

I run with

sbt "run-main mycompany.myapp.SimpleMain"

output:

[info] Running mycompany.myapp.SimpleMain 
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
simple println test
14:12:14.508 [run-main-0] ERROR mycompany.myapp.SimpleMain$ - this is a logger.error message
successfully opened log4j2.xml on classpath
[success] Total time: 4 s, completed Feb 10, 2017 2:12:14 PM
clay
  • 18,138
  • 28
  • 107
  • 192
  • Try adding `log4j.configurationFile=path/to/log4j2.xml` to your classpath when initializing SBT. – Yuval Itzchakov Feb 10 '17 at 20:22
  • 1
    I shouldn't need to do that. I should just have the file on the classpath. – clay Feb 10 '17 at 20:37
  • 1
    [Are you sure?](http://stackoverflow.com/questions/28572783/no-log4j2-configuration-file-found-using-default-configuration-logging-only-er) – Yuval Itzchakov Feb 10 '17 at 20:41
  • 1
    My colleague opted to use log4j2 and ergonomics has been poor for two years. `-D` for the config feels so 1999. – som-snytt Feb 10 '17 at 20:56
  • I can switch everything over to logback and put a logback.xml file in the resources directory and it works perfectly as expected for such a simple scenario. – clay Feb 10 '17 at 23:54
  • That's very strange indeed. Please try debugging the Log4j2 initialization by setting system property `-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE`. – Remko Popma Feb 11 '17 at 04:28
  • 1
    @som-snytt I created https://issues.apache.org/jira/browse/LOG4J2-1811 to improve Log4j2 ergonomics. Please add your feedback & ideas. – Remko Popma Feb 12 '17 at 06:00

2 Answers2

0

Is it possible that the classloader for the SimpleMain application is different from the classloader for slf4j? Please try debugging the Log4j2 initialization by setting system property org.apache.logging.log4j.simplelog.StatusLogger.level to TRAC‌​E.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

Is log4j2.xml being placed at the root of your jar? I am surprised getResourceAsStream is working. You have it specified as a relative path so it should be finding /mycompany/myapp/log4j2.xml.

rgoers
  • 8,696
  • 1
  • 22
  • 24