Convert log4j.properties file for Tomcat to work with Log4j2

I am trying to configure Tomcat 8 to use Log4j2 for logging.

I found this link to Log in to Tomcat using Log4j . It provides an example log4j.properties file that configures Log4j to match Tomcat's internal registration. Most of this looks pretty simple to convert for Log4j2, but the section at the end that displays the loggers in applications puzzles me:

# Configure which loggers log to which appenders log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\ INFO, MANAGER log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\ INFO, HOST-MANAGER 

Has anyone converted this configuration to work with Log4j2? I work from the Log4j2 configuration documentation and read the Log4j2 Architecture page, but I did not find much material on how to do this container mapping in Log4j2.

I believe that I could make a separate configuration for each container, but I would prefer to keep it in one place, as in the Log4j configuration sample.

+7
logging tomcat log4j2
source share
2 answers

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/ :

  • tomcat-juli.jar

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.jar
  • log4j-1.2-api-2.1.jar
  • log4j-api-2.1.jar
  • log4j-core-2.1.jar
  • log4j-jcl-2.1.jar
  • log4j-jul-2.1.jar
  • log4j-web-2.1.jar
  • tomcat-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).

+13
source share

I followed the Login to Tomcat link using Log4j and added tomcat-juli.jar to the bin directory and I also added tomcat-juli-adapters.jar to the lib directory. After that I added log4j-api-2.1.jar, log4j-core-2.1.jar and log4j-1.2-api-2.1.jar to the lib directory.

After that I added log4j2.xml to the lib directory. I saved the configuration quite simply, using a sliding configuration, depending on the time and size, which fastens the archive logs:

 <?xml version="1.0" encoding="UTF-8"?> <Configuration status="warn" name="catalina" packages=""> <Appenders> <RollingRandomAccessFile name="catalina" fileName="${sys:catalina.base}/logs/catalina.log" filePattern="${sys:catalina.base}/logs/catalina/$${date:yyyy-MM}/catalina-%d{yyyy-MM-dd}-%i.log.zip"> <PatternLayout> <Pattern>%d{MMM d, yyyy HH:mm:ss}: %5p (%F:%L) - %m%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="250 MB" /> </Policies> <DefaultRolloverStrategy max="100" /> </RollingRandomAccessFile> </Appenders> <Loggers> <!-- default loglevel for emaxx code --> <logger name="org.apache.catalina" level="info"> <appender-ref ref="catalina" /> </logger> <Root level="info"> <appender-ref ref="catalina" /> </Root> </Loggers> </Configuration> 

This way you get all the entries in the catalina.log file. I'm still working on making accessLog the same.
EDIT: I found this site. this way you can directly register directory access. (I could not get him to log into his own appender)

I was not worried about registering a manager and host manager, since we don’t have them in production environments, but they can just go into the catalina.log directory. I have not tested it.

This has been tested on tomcat-7.0.42, it should work on tomcat8 as well.

+1
source share

All Articles