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.

有帮助吗?

解决方案

This may be helpful:

  • Log4j2 will first look for log4j2-test.xml in the classpath
  • if that file is not found, it will look for log4j2.xml in the classpath

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.

其他提示

Log4j2 supports "Composite Configuration" which exactly matches your requirement. All you need to do is provide path to multiple files in log4j.configurationFile property. This can be passed from command line or added to log4j2.component.properties file in your application.


References

There are two step you can try to solve for your issue

  • Create your own configuration file with your custom name(eg: xyz.properties/.xml)
  • You must add the following line to your java runtime command

cmd> java -Dlog4j.configuration=location/xyz.properties

If you use diffent name for configuration rather log4j.properties/.xml file you need to configure that file at runtime by above command for more info have a look here..

Correct format for using an alternate XML file to log4j2.xml:

java -Dlog4j.configurationFile=./location/log4j2-custom.xml

Assuming ./location/log4j2-custom.xml exists and is the new XML to replace log4j2.xml in this run

See: https://github.com/kamalcph/Log4j2Examples/blob/master/src/main/java/in/co/nmsworks/log4j2/examples/CompositeConfigurationExample.java

Referencing https://logging.apache.org/log4j/2.x/manual/configuration.html states that you can add multiple comma separated files under log4j2.configurationFile property.

To use multiple configuration files, depending on the environment you must set the.

for example:

if (env.equals("DEV")) {
 setConfigFile("log4j2-dev.xml");
}

public static void setConfigFile(String logConfigFile) {
        File file = new File(logConfigFile);
        LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
        context.setConfigLocation(file.toURI());
    }
first configure application.yaml file
spring:
  profiles:
    active: dev

---
spring:
  message: running in the dev profile    //just to output the message in the log
  profiles: dev
logging.config: classpath:log4j2-dev.xml

---
spring:
  profiles: prod
logging.config: classpath:log4j2-prod.xml



and create these similar files in your classpath
* log4j2-dev.xml
* log4j2-prod.xml
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top