Search code examples
web-applicationslog4j2tomcat8

How to configure log4j2 for a web application in Tomcat 8?


I'm deploying a web service as a web app (war) over Tomcat 8.0.24. Application jars have already been tested and they work fine. Now for configuring and testing log4j purposes I provoked a runtime error (no db connected).

I'm trying to set log4j2 for application logging only (not for tomcat) and I'm using Netbeans 8.0.2 to build war.

I have included following jars in classpath:

log4j-api-2.8.jar
log4j-core-2.8.jar
log4j-web-2.8.jar
log4j-jcl-2.8.jar   
commons-logging.jar 

And log4j2.xml in WEB-INF, with only root logger configured to catch everything coming from web app:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration strict="true" status="TRACE">
  <Properties>
    <Property name="filename">C:\logs\Myapp.log</Property>
  </Properties>
  <Appenders>

    <Appender type="Console" name="Console" target="SYSTEM_OUT">
      <Layout type="PatternLayout">
        <Pattern>%d{HH:mm:ss.SSS} - %-5level %logger - %msg%n</Pattern>
      </Layout>
    </Appender>
    <Appender type="File" name="FileAppender" fileName="${filename}" append="true" immediateFlush="true">
      <Layout type="PatternLayout">
        <Pattern>%d %p %logger -%C- %m%n</Pattern>
      </Layout>
    </Appender>
  </Appenders>
  <Loggers>
    <Root level="ERROR">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="FileAppender" level="WARN"/>
    </Root>
  </Loggers>
 </Configuration>

I have also configured catalina.properties, just in case, to override default jarsToSkip:

tomcat.util.scan.StandardJarScanFilter.jarsToScan=log4j-core*.jar,log4j-taglib*.jar,log4j-api*.jar,log4j-web*.jar,log4j-jcl*.jar`

I believe log4j is being properly initialized as catalina*.log states, due to status="TRACE":

2017-02-08 14:06:52,355 http-nio-8080-exec-8 DEBUG Log4jServletContextListener ensuring that Log4j starts up properly.
2017-02-08 14:06:52,357 http-nio-8080-exec-8 DEBUG Log4jServletFilter initialized.

And also because log file is created.

However, all runtime error messages coming from application (web service jar) are ending into catalina*.log instead of file appender I have configured. Besides, those error messages in catalina*.log are not following any pattern defined in Appenders' definitions, so it basically seems like log4j2 is being ignored.

What am I doing wrong?


Solution

  • In log4j's documentation I found that there's a log4j 1.2 API under Components and it states that it "allows applications coded to use Log4j 1.2 API to use Log4j 2 instead."

    Well, I found that application's jars were developed using previous version of log4j. So I included log4j-1.2-api-2.8.jar and removed log4j-jcl-2.8.jar and commons-logging.jar and that did the trick. Now application's log messages are sent to file configured as appender.

    Maybe it's worth mentioning that first test after adding log4j-1.2-api-2.8.jar didn't work, so I guess it makes kind of conflict with the jars I removed later.