log4j2 writing multiple files for multiple webapps in tomcat

As we moved to the latest version of the log4j. We were using log4j 1.x and now we upgraded to log4j 2.x.
We have 4 webapps in single tomcat container and they all share the same log4j2.xml. This log4j2.xml is provided at the startup of the tomcat container using the property “log4j.configurationFile”. property In log4j2.xml we had the appender to write all the logs to a file. Here you will the configuration file used.
When we saw the log outputs we were happy that the logs are being written to the file and the history files were also created. But after a day of use on our development system we had to search for some logs and found that these logs from a particular war was not written into the tomcat.log(file configured in the log42.xml). So as we were looking for the lost logs we soon fond that these logs were somehow written into the archive files. Log4j 2 created four files in the archive folder at once and wrote logs of  different webapps in the specific history files.
So here was the problem that all the logs were not written in the tomcat.log where it was configured to be written into. But into the history folder with different names.
So as we investigated the issue we came to know that all the wars had there own jars for the log4j 2. And here the problem is explained in the log4j 2 website as.  An extract from the site“In this scenario there are multiple WAR files deployed into a single container. Each of the applications should use the same logging configuration and share the same logging implementation across each of the web applications. When writing to files and streams each of the applications should share them to avoid the issues that can occur when multiple components try to write to the same file(s) through different File objects, channels, etc.”

 So here we came to the conclusion that all the wars should not include the log4 implementation. We included the log4j 2 jars in the tomcat lib folder. This solved our problem and all the logs were written into the file as we expected.
  Job well done!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.