Skip to content

Instantly share code, notes, and snippets.

@bmaupin
Last active November 15, 2024 11:11
Show Gist options
  • Save bmaupin/475a0cd6e8b374d876f5085846761fb6 to your computer and use it in GitHub Desktop.
Save bmaupin/475a0cd6e8b374d876f5085846761fb6 to your computer and use it in GitHub Desktop.
Log4j2.xml for Tomcat
<?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}.log.gz">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="catalina.*.log.gz" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="LOCALHOST"
fileName="${logdir}/localhost.log"
filePattern="${logdir}/localhost.%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="localhost.*.log.gz" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="MANAGER"
fileName="${logdir}/manager.log"
filePattern="${logdir}/manager.%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="manager.*.log.gz" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="HOST-MANAGER"
fileName="${logdir}/host-manager.log"
filePattern="${logdir}/host-manager.%d{yyyy-MM-dd}.log.gz">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="host-manager.*.log.gz" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</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>
@chriskriel
Copy link

Thanks. It does exactly what I need without changing a line!

@ejgutierrez74
Copy link

Can you translate this file to log4j2.properties style ? Thanks

@brunogenovese
Copy link

It could easily be my own error but I think this is no longer valid, possibly due to changes made by Tomcat and log4j in response to the recent log4j2 security issues.

It should have been a no-brainer using the following steps, but it did not work:

  • used the latest Tomcat 9 (9.0.56)
  • deleted tomcat/conf/logging.properties
  • added log4j-api-2.17.1.jar, log4j-core-2.17.1.jar, log4j-appserver-2.17.1.jar to tomcat/lib
  • added your log4j2.xml verbatim to tomcat/lib

No log files get created, the Tomcat console shows what looks like original JULI logging.

@azhar2205
Copy link

azhar2205 commented Jan 8, 2022

This worked for me on Tomcat 8.0.30. In addition to these steps also add the disruptor-3.4.4.jar to the $CATALINA_HOME/log4j2/lib folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment