ppkarwasz commented on issue #1258:
URL: 
https://github.com/apache/logging-log4j2/issues/1258#issuecomment-1412634661

   @mariusv5,
   
   Your problem is caused by multiple `log4j-api` and `log4j-core` jars being 
loaded into your Tomcat system (your logs show 3 instances of 
`ClassLoaderContextSelector`, one for each web application). Each one opens a 
file descriptor to your log file and each one tries to rotate it. As you can 
imagine, this does not end well.
   
   Log4j2 is perfectly capable of sharing the same log file between logging 
context, if a **single** copy of `log4j-core` is deployed in your system. 
Technically each logging context will then have **its own** copy of 
`RollingFileAppender`, but the engine that opens files and performs the 
rotation 
([`RollingFileManager`](https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/RollingFileManager.html))
 will be shared.
   
   Unlike full application servers such as Wildfly, Tomcat has not easy way to 
share a **single** library between web applications. In order to do that you 
need to install a set of Log4j2 artifacts in the `$CATALINA_BASE/lib` folder 
and do **one** of the following:
   
   1. Remove the `log4j-*` artifacts from your web applications (personally I 
don't like the idea of modifying web applications),
   2. Switch the delegation model of all web application classloaders to 
"parent first" (this might break some apps if they need different versions of 
the same library), by adding:
      ```lang-xml
      <Loader delegate="true"/>
      ```
      to their context configuration (e.g. in 
`$CATALINA_BASE/conf/context.xml`). Cf. [Tomcat 
documentation](https://tomcat.apache.org/tomcat-9.0-doc/config/loader.html).
   3. Use a custom classloader that looks up `org.apache.logging.log4j.*` 
classes in the parent classloader **first**, but uses the usual delegation for 
the other classes. This is basically what Tomcat does with 
`org.apache.catalina.*` and other internal classes.
   
   You can find such a classloader in my personal repository (cf. 
[`Log4jWebappClassloader`](https://github.com/copernik-eu/log4j-plugins/blob/main/log4j-tomcat/src/main/java/eu/copernik/log4j/tomcat/Log4jWebappClassLoader.java)).
 You can find it on Maven Central under the coordinates:
   ```lang-xml
   <dependency>
       <groupId>eu.copernik</groupId>
       <artifactId>log4j-tomcat</artifactId>
       <version>2.18.0</version>
   </dependency>
   ```
   
   To use it, place it in the `$CATALINA_BASE/lib` folder together with 
`log4j-api` and `log4j-core` and add:
   ```lang-xml
   <Loader loaderClass="eu.copernik.log4j.tomcat.Log4jWebappClassLoader"/>
   ```
   to your `$CATALINA_BASE/conf/context.xml` file.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to