log4j2 config file is not being recognized

TIMBERings picture TIMBERings · Mar 1, 2014 · Viewed 8.5k times · Source

I'm trying to get log4j2 setup in my Geb framework. For the life of me I can't figure out how to put this together. I have a log4j2.xml (not log4j.xml) file setup with the logger "Selenium". In DefaultPage I try to get the Logger with the name Selenium from the config file LogManager.getLogger("Selenium").

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
      <Logger name="Selenium" level="TRACE">
          <AppenderRef ref="Console"/>
      </Logger>
    <Root level="TRACE">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

DefaultPage.groovy

class DefaultPage extends geb.Page {
    static content = {
        pageId(wait: true) { $("meta", name: "pageId").getAttribute("content") }
        pageHeading(wait: true) { $("meta", name: "heading").getAttribute("content") }
    }

    static final Logger logger = LogManager.getLogger("Selenium")

    /**
     * Uses jquery to create a mouseover on an element
     * @param element The jquery element created in the page object.
     */
    static def mouseOver(element) {
        logger.error("Hovering over ${element}")
        element.jquery.mouseover()
    }
}

The test executes and output is written to STDERR, which is expected because I have logger.error. However, it doesn't hold format for the date. Additionally I have other classes calling this logger with .info and those are not seen in STDOUT. When I debug the level of the logger is ERROR, not TRACE.

Here's my file structure:

functional
|
 --src
   |
   |--test
      |
      |--groovy
      |     |
      |     |--com.x.functional
      |         |
      |         |--pages
      |              |
      |              |--DefaultPage.groovy
      |              |--Other classes that want to use log4j2.
      |
      |--resources
             |
             |--log4j2.xml

Thank you.

EDIT Changing log4j to log4j2. Adding Configuration status = TRACE. Modified date format for milliseconds with ',' instead of '.'

Answer

Remko Popma picture Remko Popma · Mar 1, 2014

Log4J2 will look for a config file named log4j2.xml in the classpath. If it cannot find this file it will try a few alternatives (see the configuration manual page), but log4j.xml is not recognized by default. (It is possible to tell log4j2 to take some specified file as the config: http://logging.apache.org/log4j/2.x/faq.html#config_location )

If no config file is found, a default configuration is installed that will log ERROR and higher events to StdErr.

I suspect this is what is happening. The simplest way to remedy this is to rename your config file log4j2.xml and place it in the classpath. You can then enable log4j2 internal logging by specifying <Configuration status="trace"> to further troubleshoot if necessary.