After I asked this question, I spent a little more time setting up log4j2, and this is the log4j2.xml file I came up with. It simulates the configuration described in Logging into Tomcat using Log4j . It uses multiple logs to route messages to separate log files.
<?xml version="1.0" encoding="utf-8"?> <Configuration status="info"> <Properties> <Property name="logdir">${sys:catalina.base}/logs</Property> <Property name="layout">%d [%t] %-5p %c- %m%n</Property> </Properties> <Appenders> <Console name="CONSOLE" target="SYSTEM_OUT"> <PatternLayout pattern="${layout}"/> </Console> <RollingFile name="CATALINA" fileName="${logdir}/catalina.log" filePattern="${logdir}/catalina.%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${layout}"/> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB"/> </Policies> <DefaultRolloverStrategy max="10"/> </RollingFile> <RollingFile name="LOCALHOST" fileName="${logdir}/localhost.log" filePattern="${logdir}/localhost.%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${layout}"/> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB"/> </Policies> <DefaultRolloverStrategy max="10"/> </RollingFile> <RollingFile name="MANAGER" fileName="${logdir}/manager.log" filePattern="${logdir}/manager.%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${layout}"/> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB"/> </Policies> <DefaultRolloverStrategy max="10"/> </RollingFile> <RollingFile name="HOST-MANAGER" fileName="${logdir}/host-manager.log" filePattern="${logdir}/host-manager.%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${layout}"/> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="1 MB"/> </Policies> <DefaultRolloverStrategy max="10"/> </RollingFile> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="CATALINA"/> </Root> <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="info" additivity="false"> <AppenderRef ref="LOCALHOST"/> </Logger> <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]" level="info" additivity="false"> <AppenderRef ref="MANAGER"/> </Logger> <Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]" level="info" additivity="false"> <AppenderRef ref="HOST-MANAGER"/> </Logger> </Loggers> </Configuration>
However, for this to work properly, a bunch of other files are correctly configured. The tomcat 7 internal log with log4j2.xml post from Paramita Banerjee was helpful with this.
This file is located in CATALINA_HOME/bin/ :
If you pull this from the Maven repository, you will get a file called something like tomcat-extras-juli-8.0.15.jar (current when I wrote this). However, it must be renamed to tomcat-juli.jar - Tomcat installation scripts use this name in the CLASSPATH configuration.
These files are included in CATALINA_HOME/lib/ :
commons-logging-1.2.jarlog4j-1.2-api-2.1.jarlog4j-api-2.1.jarlog4j-core-2.1.jarlog4j-jcl-2.1.jarlog4j-jul-2.1.jarlog4j-web-2.1.jartomcat-juli-adapters-8.0.15.jar
log4j-web-2.1.jar may not be needed here (it just needs to be deployed using your web application) - its use is described in Using Log4j 2 in web applications . log4j-1.2-api-2.1.jar is only required if you have applications that use the older log4j 1.2 interface.
In CATALINA_BASE/conf I turned off logging.properties .
I used the following setenv.sh script to determine the CLASSPATH and LOGGING_MANAGER correct environment variables for Tomcat. It goes either to CATALINA_BASE/bin or to CATALINA_HOME/bin . (I put it in CATALINA_HOME / bin.) Run by Tomcat startup scripts, if present. You may prefer something simpler, but it matches the style of the startup scripts.
LOG4J_JARS="log4j-core-2.1.jar log4j-api-2.1.jar log4j-jul-2.1.jar" # make log4j2.xml available if [ ! -z "$CLASSPATH" ] ; then CLASSPATH="$CLASSPATH": ; fi CLASSPATH="$CLASSPATH""$CATALINA_BASE"/lib # Add log4j2 jar files to CLASSPATH for jar in $LOG4J_JARS ; do if [ -r "$CATALINA_HOME"/lib/"$jar" ] ; then CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/lib/"$jar" else echo "Cannot find $CATALINA_HOME/lib/$jar" echo "This file is needed to properly configure log4j2 for this program" exit 1 fi done # use the logging manager from log4j-jul LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager"
As Nick noted in his answer, access log. I also did not try to do anything about it.
I hope others find this helpful. Looking back, it seems pretty simple. However, there are many parts that must be βrightβ for it to work, and this is a challenge (at least for a beginner).