11

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user3093295
  • 133
  • 1
  • 1
  • 6
  • Log4j loads configuration files from the classpath. If you put the test code classpath before the library classpath the log4j configuration should be taken from the test code. Here is a [link](http://stackoverflow.com/questions/2772466/where-to-place-log4j-xml) to the stackoverflow question which can be helpful. – Alexey Gavrilov Apr 21 '14 at 19:44
  • Thanks for the reply Alexey, but I don't think I was clear enough in my question. I've edited it to try to be more clear. – user3093295 Apr 21 '14 at 20:08

7 Answers7

9

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
zendu
  • 1,108
  • 1
  • 14
  • 36
8

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.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Thanks, I did in fact wind up with merging both the test and the distribution log4j2 files. Not really what I wanted, but apparently there's nothing better. I did try using Xinclude, but could not make it work. Not important enough to spend more time trying to get Xinclude strategy to work. – user3093295 Apr 28 '14 at 14:48
3

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..

loknath
  • 1,362
  • 16
  • 25
  • Where do you change your "java runtime command"? I tried Project > Preferences > Run/Debug Settings , picked some configurations, clicked Edit, Arguments tab, VM arguments. – Noumenon Aug 20 '15 at 11:05
0

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

habeeb
  • 1
0

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

0

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());
    }
0
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