Question

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 '.'

Was it helpful?

Solution

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.

OTHER TIPS

Old question but I'll answer in case someone keeps looking for how to solve this.

As TIMBERings says, is recommended to put the file in a Resources folder. If you're working with eclipse, you need to add the Resources folder to the sources at the Java Build Path.

There's no need to specify <Configuration status="trace">, <Configuration> is enough.

You can put "." or "," between ss and SSS %d{HH:mm:ss.SSS} (it's just formatting).

Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top