javalogginglog4j2xinclude

How to use multiple configuration files for Log4j2?


I am writing Java code that tests a Java library. The library includes its own Log4j2 configuration as part of the distribution.

I would like to use Log4j2 in my test code without modifying the library's configuration.

Is there a way to have a separate Log4j2 configuration for my test code?

This is all running as command-line Java, no servers or web involvement at all.


EDIT

What I want is to be able to configure loggers, appenders, etc for the test code to use, and at the same time have the library code use its own separate configuration file for its logging.

The idea is to use Log4j2 in my test code, but without having to change the library's configuration file. Since the library configuration file is part of the library's distribution, I don't want to change it for testing.


Solution

  • This may be helpful:

    So one option is to copy the library's configuration (log4j2.xml) to log4j2-test.xml and add your own configuration to log4j2-test.xml.

    Furthermore, Log4j2 supports XInclude in XML configuration, so you could use that feature to avoid duplicating the library's configuration in your log4j2-test.xml.