What is the best way to go about implementing an external library that uses Log4j2 when the main application that uses this external library also has their own Log4j2 implementation?
Asked
Active
Viewed 164 times
1
-
1By _"has their own Log4j2 implementation"_ you mean they both use the Log4j 2.x API? In that case, once you add a Log4j2 implementation to the runtime, both will log correctly. – Piotr P. Karwasz Feb 08 '22 at 08:27
-
I tried, however, the main application is unable to find the logger. The current setup is: The external library is using the Configurator to initialize Log4j2. The main application is using Log4j2.xml configuration file. When running the main application, the external library's configurator will overwrite the Log4j2.xml configuration and thus making it unable to find the logger. – Jerome Sin Feb 09 '22 at 03:08
1 Answers
1
I did so by programmatically creating a new LoggerContext in the external library, and using that LoggerContext for the Log4j2 logging. Since it is a new LoggerContext, it should not interfere/conflict with the main application's Log4j2 xml configuration file or configurator.

Jerome Sin
- 21
- 3