Question

I'm using Tomcat 8.0.3 and log4j 2.0-rc1 to deploy a GWT application (servlet 2.5).

I believe I fixed the usual log configuration errors, but still nothing is logged to the console or file.

Head of my web.xml:

<listener>
    <listener-class>org.apache.logging.log4j.core.web.Log4jServletContextListener</listener-class>
</listener>

<filter>
    <filter-name>log4jServletFilter</filter-name>
    <filter-class>org.apache.logging.log4j.core.web.Log4jServletFilter</filter-class>
</filter>

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>file:///C:/Users/name/Desktop/log4j2.xml</param-value>
</context-param>

<filter-mapping>
    <filter-name>log4jServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

This is the log4j2.xml configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%p] %c{1}: %m%n"/>
        </Console>
        <File name="infoFile" fileName="${sys:catalina.home}/logs/info.log" >
            <PatternLayout pattern="[%p] %c{1}: %m%n"/>
        </File>
    </Appenders>

    <Loggers>
        <Root level="info">
            <Appender-Ref ref="Console"/>
            <Appender-Ref ref="infoFile"/>
        </Root>
    </Loggers>
</Configuration>

I added both the log4j2 api and core to my Maven dependencies and I can see them exploded in WEB-INF\classes\. I've also removed them from the skipJar list in catalina.properties.

When the war is deployed, no errors or warnings are thrown in the localhost or catalina log. This is the localhost log:

INFO  ContextListener: contextInitialized()
INFO  SessionListener: contextInitialized()
INFO  Log4jServletContextListener ensuring that Log4j starts up properly.
INFO  Log4jServletFilter initialized.

The strange thing is that the info.log file is created, but never written to.
I'm also using Hibernate whose logs do show up in the console, could this be the problem?

Finally, this is how I'm using the logger:

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

private static final Logger LOG = LogManager.getLogger(MyClass.class.getName());

public void myMethod() {
    LOG.info("Logging some message");
}

(The log4j2 config file is temporarily on my desktop because it seems that I can't use spaces in the path)

Was it helpful?

Solution

As far as I can see your configuration is correct and all is set up as described in the log4j2 manual page for web apps.

One thing you could try is putting the log4j2.xml config file in the classpath (under WEB-INF/classes) and removing the log4jConfiguration context-param setting. But since you are seeing an info.log file being created, it is likely that the log4j2.xml file is already discovered correctly, so putting it in the classpath may not help. (Worth a try though.)

Two other reasons I can think of why nothing appears in the log file: log output is buffered and the buffer is not flushed on every event. However, the default for FileAppender is immediateFlush=true, so that would mean a new bug was introduced in rc1. This is possible, but not likely (current trunk source looks correct).

Finally, would it be possible that your method that does the logging (myMethod in your example above) is not called? Is there a way to verify that this method was called?

OTHER TIPS

In my case, I has configured log4j2.xml file correctly, but while actually logging, I mistakenly used the @Slf4j annotation in Lombok. Changing it to @Log4j2 resolved the issue.

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