Question

I have a SBT project with the following log4j.properties file (in src/main/resources):

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

build.sbt

 "com.typesafe" %% "scalalogging-log4j" % "1.1.0",
 "org.slf4j" % "slf4j-log4j12" % "1.7.5",
 "org.apache.logging.log4j" % "log4j-core" % "2.0-beta4",
 "org.apache.logging.log4j" % "log4j-api" % "2.0-beta4"

The following code snippet fails to output anything to the console when executing sbt run:

 class Retrieval extends Logging {
   def get(userID: Int): Seq[String] = {
     logger.info("test")
   }
 }

Changing to logging.error("test") works fine. Any idea what's going on?

Was it helpful?

Solution

You are using log4j2 (I recommend upgrading to the more recent log4j-2.0-RC1, btw), but your configuration is for the legacy log4j-1.2.

Log4j2 is looking for a log4j2.xml config file and ignores any log4j.xml or log4j.properties files. If log4j2 cannot find a config file, it will use a default configuration which will log only ERROR-level messages to the console. I think this is what is happening.

To solve this, create below file named log4j2.xml, and put it in the classpath.

<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="STDOUT"/>
    </Root>
  </Loggers>
</Configuration>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top