log4j2 config file is not being recognized
Asked Answered
H

2

6

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

Honor answered 28/2, 2014 at 23:36 Comment(1)
Pattern for date is %d{HH:mm:ss,SSS} (note the comma instead of . for millisecs)Jeanicejeanie
G
5

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.

Guyette answered 1/3, 2014 at 4:36 Comment(3)
I must have been reading the wrong documentation, I thought it said log4j.xml. I initially had log4j2.xml, it's now log4j2.xml again. <Configuration status="TRACE"> is in place instead of ERROR, however no luck. I'm still only getting ERROR Output, which would be the default configuration. I have also made the date pattern change. So, all issues addressed, still have the same issue. I created another logger with a name that wasn't in the log4j2 config. The selenium logger and the x logger were setup the same. So we are hitting default.Honor
Do you have both log4j-api and the log4j-core jar files in the classpath? Also, is the log4j2.xml file in the classpath? If the answer to both of the above is yes, then after changing to <Configuration status="trace">, you should have started to see log4j2 internal logging on your console (like initialization stuff when the log4j2.xml config file is parsed, which appenders are installed etc). Are you seeing that? Once that is fixed I think the trace level logging in your app will also work.Guyette
I know this is an old post, but this saved me a whole lot of frustration.Conception
P
1

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.

Pyramidon answered 12/12, 2018 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.