tomcat 7 internal logging with log4j2.xml

♀尐吖头ヾ 提交于 2019-11-28 10:02:11

I took the following steps and it worked for me.

  1. Put the following jars in $CATALINA_HOME/lib
    • log4j 2 core (log4j-core-2.4.1.jar)
    • log4j 2 api (log4j-api-2.4.1.jar)
    • log4j 2 bridge for log4 j 1.0 (log4j-1.2-api-2.4.1.jar)
    • tomcat-juli-adapters.jar from tomcat extras
  2. Replace existing $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from tomcat extras
  3. Delete the file $CATALINA_HOME/conf/logging.properties
  4. Put the new log4j 2 config file (log4j2.xml) in $CATALINA_HOME/lib

The trick is to follow the official tomcat 7 documentation to setup log4J 1.X but instead use log4j2 artifacts instead. Also this solution does not require any changes in $CATALINA_HOME/bin/catalina.sh or any other files $CATALINA_HOME/bin

Paramita Banerjee

My mistake, I needed to include $CATALINA_BASE/lib in classpath for log4j2.xml to be picked up.

rjdkolb

Adapt the following for your installation:

My Tomcat 8.5.x is located at /opt/tomcat/

1. Copy log4j2 jar files to /opt/tomcat/lib/

(For Windows use copy)

2. Create /opt/tomcat/conf/log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="LOGJ2 %d [%-6p] %c{1} – %m%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="*" level="trace">
            <AppenderRef ref="Console"/>
        </Logger>

        <Root level="info">
            <appenderRef ref="Console" level="info"/>
        </Root>
    </Loggers>
</Configuration>

3. Create or edit /opt/tomcat/bin/setenv.sh or /opt/tomcat/bin/setenv.bat

Without the CLASSPATH set in setenv.sh/bat the initial Tomcat boot loader will not be able to access the class org.apache.logging.log4j.jul.LogManager and log4j2 classes. Setting CLASSPATH before starting Tomcat does not effect the initial Tomcat boot loader since catalina.sh/bat unsets CLASSPATH before running setenv.sh/bat

#The environment variable CLASSPATH is unset in catalina.sh/catalina.bat
CLASSPATH=/opt/tomcat/lib/log4j-api-2.9.1.jar:/opt/tomcat/lib/log4j-core-2.9.1.jar:/opt/tomcat/lib/log4j-jul-2.9.1.jar
JAVA_OPTS=-Dlog4j.configurationFile=/opt/tomcat/conf/log4j2.xml
LOGGING_MANAGER=-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager

4. View logs in /opt/tomcat/log/catalina.out and notice the LOGJ2 prefix from the log42.xml

LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – Server built:          Sep 28 2017 10:30:11 UTC
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – Server number:         8.5.23.0
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – OS Name:               Linux
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – OS Version:            4.4.0-93-generic
LOGJ2 2017-10-12 08:47:37,797 [INFO  ] VersionLoggerListener – Architecture:          amd64
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!