SBT run differences between scala and java?

Posted by Eric Cartner on Stack Overflow See other posts from Stack Overflow or by Eric Cartner
Published on 2012-12-19T15:02:55Z Indexed on 2012/12/19 17:03 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

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"
)

When I run the main() defined in src/main/scala/logtest/Foo.scala:

package logtest

import org.apache.logging.log4j.{Logger, LogManager}

object Foo {
  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.")
  }
}

I get the output I was expecting given that src/main/resources/log4j2.xml sets the root logging level to trace:

[info] Running logtest.Foo 
08:39:55.627 [run-main] TRACE logtest.Foo$ - Entering application.
08:39:55.630 [run-main] TRACE logtest.Bar -  entry
08:39:55.630 [run-main] ERROR logtest.Bar - Did it again!
08:39:55.630 [run-main] TRACE logtest.Bar -  exit with (false)
08:39:55.630 [run-main] ERROR logtest.Foo$ - Didn't do it.
08:39:55.630 [run-main] TRACE logtest.Foo$ - Exiting application.

However, when I run the main() defined in src/main/java/logtest/LoggerTest.java:

package logtest;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class LoggerTest {
  private static Logger logger = LogManager.getLogger(LoggerTest.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.");
  }
}

I get the output:

[info] Running logtest.LoggerTest 
ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger
ERROR Bar Did it again!
ERROR LoggerTest Didn't do it.

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.LoggerTest to be the main class. Running it from the command line produced correct results:

Eric-Cartners-iMac:target ecartner$ java -jar "Logging Test-assembly-0.0.jar" 
10:52:23.220 [main] TRACE logtest.LoggerTest - Entering application.
10:52:23.221 [main] TRACE logtest.Bar -  entry
10:52:23.221 [main] ERROR logtest.Bar - Did it again!
10:52:23.221 [main] TRACE logtest.Bar -  exit with (false)
10:52:23.221 [main] ERROR logtest.LoggerTest - Didn't do it.
10:52:23.221 [main] TRACE logtest.LoggerTest - Exiting application.

© Stack Overflow or respective owner

Related posts about java

Related posts about scala