I'm trying to follow the log4j2 configuration tutorials in a SBT 0.12.1 project. Here is my build.sbt:
name := "Logging Test"
version := "0.0"
scalaVersion := "2.9.2"
libraryDependencies ++= Seq(
"org.apache.logging.log4j" % "log4j-api" % "2.0-beta3",
"org.apache.logging.log4j" % "log4j-core" % "2.0-beta3"
)
I have two separate main classes. The first is logtest.ScalaTest
in src/main/scala/logtest/ScalaTest.scala:
package logtest
import org.apache.logging.log4j.{Logger, LogManager}
object ScalaTest {
private val logger = LogManager.getLogger(getClass())
def main(args: Array[String]) {
logger.trace("Entering application.")
val bar = new Bar()
if (!bar.doIt())
logger.error("Didn't do it.")
logger.trace("Exiting application.")
}
}
and the second is logtest.JavaTest
in src/main/java/logtest/JavaTest.java:
package logtest;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class JavaTest {
private static Logger logger = LogManager.getLogger(JavaTest.class.getName());
public static void main(String[] args) {
logger.trace("Entering application.");
Bar bar = new Bar();
if (!bar.doIt())
logger.error("Didn't do it.");
logger.trace("Exiting application.");
}
}
If I run logtest.ScalaTest.main()
from inside sbt I get the output I was expecting given that src/main/resources/log4j2.xml sets the root logging level to trace:
> run-main logtest.ScalaTest
[info] Running logtest.ScalaTest
10:26:23.730 [run-main] TRACE logtest.ScalaTest$ - Entering application.
10:26:23.733 [run-main] TRACE logtest.Bar - entry
10:26:23.733 [run-main] ERROR logtest.Bar - Did it again!
10:26:23.733 [run-main] TRACE logtest.Bar - exit with (false)
10:26:23.733 [run-main] ERROR logtest.ScalaTest$ - Didn't do it.
10:26:23.733 [run-main] TRACE logtest.ScalaTest$ - Exiting application.
[success] Total time: 0 s, completed Dec 21, 2012 10:26:23 AM
However, when I run logtest.JavaTest.main()
from inside sbt I get different output
> run-main logtest.JavaTest
[info] Running logtest.JavaTest
ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger
ERROR Bar Did it again!
ERROR JavaTest Didn't do it.
[success] Total time: 0 s, completed Dec 21, 2012 10:27:29 AM
From what I can tell, ERROR StatusLogger Unable to ...
is usually a sign that log4j-core is not on my classpath. The lack of TRACE messages seems to indicate that my log4j2.xml settings aren't on the classpath either. Why should there be any difference in classpath if I'm running Foo.main versus LoggerTest.main? Or is there something else causing this behavior?
Update
I used SBT Assembly to build a fat jar of this project and specified logtest.JavaTest to be the main class. Running it from the command line produced correct results:
$ java -jar "Logging Test-assembly-0.0.jar"
10:29:41.089 [main] TRACE logtest.JavaTest - Entering application.
10:29:41.091 [main] TRACE logtest.Bar - entry
10:29:41.091 [main] ERROR logtest.Bar - Did it again!
10:29:41.091 [main] TRACE logtest.Bar - exit with (false)
10:29:41.091 [main] ERROR logtest.JavaTest - Didn't do it.
10:29:41.091 [main] TRACE logtest.JavaTest - Exiting application.
GitHub Example
Following Edmondo1984's suggestion, I put together a complete example and put it up on github.
$ sbt run
and then picking which main class to run when prompted by sbt. – Quigibo$ sbt assembly
and then$ java -jar "Logging Test-assembly-0.0.jar"
. – Quigibo> run-main logtest.Foo
- this runs the scala main and produces correct results. #2 again, inside sbt ` > run-main logtest.LoggerTest` - this runs the java main and produces the bad results. #3 two parts: a) from inside sbt> assembly
- this produces the "fat jar" and b) from shell$ java -jar "Logging Test-assembly-0.0.jar"
- this runs the main class designated in build.sbt bymainClass in assembly := ...
. – Quigibo