Learn to use a different Log4j2 configuration file for JUnit tests is a recommended approach. We can apply two ways to configure Log4j2 specific to tests and that is different from the production logging config file.
1. Place log4j2-test.xml in test/resources Folder
Place the log4j2-test.xml file in ‘src/test/resources‘ folder.
By placing a log4j2-test.xml into this directory will cause it to be used instead of a log4j2.xml or log4j2.json that might be present in ‘src/main/resources‘ folder.

2. Load from external locations with ‘log4j.configurationFile‘ Property
Another way to introduce a different log configuration file is – to set log4j.configurationFile property in @BeforeAll annotation in any test class.
For example, we can create test specific logging configuration file log4j2-testConfig.xml and place it in some external folder. Now let use this file in JUnit tests.
import java.net.MalformedURLException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
public class HelloWorldTest
{
private static Logger LOGGER = null;
@BeforeClass
public static void setLogger() throws MalformedURLException
{
System.setProperty("log4j.configurationFile","log4j2-testConfig.xml");
LOGGER = LogManager.getLogger();
}
@Test
public void testOne()
{
//test code
}
}
Drop me your questions related to log4j2 configuration for junit tests in the comments section.
Happy Learning !!
Hello
I changed the settings so that the path of my files is defined in the program, with the following code:
System.setProperty(“log4j.configurationFile”,”log4j2.xml”);
System.setProperty(“pathHome”,path);
((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).reconfigure();
However, it deletes the previous messages and does not save the history of the logs, so it does not respect the other configurations, how can I do so that I only update the path of the logs? and the rest will be left with my file settings, thanks.
Hi,
I have built and tested log4j2 configuration in local and planned to deploy but when I am trying to deploy all my log4j2 changes in to test environment however I am getting below error message in jenkins log while deploying. Can you please help me resolving this issue.
To me, it seems the permission issues in the server. The Java process is not able to create the directory. Talk to your server team to allow creating directories for all users in the log folder. Once it is determined to be the root cause, ask them to limit it to system users only.
Can there be multiple configuration files under resources path? for example, i want to use one configuration file for some tests.. and some other config file for other tests..
Yes, you can place all configuration files in resources folder and load them in test classes as shown above.
thanks this works.
I had missed setting System.setProperty(“log4j.configurationFile”,”log4j2-testConfig.xml”);
Hi Lokesh,
My log4j2 configuration is set up in such a way that it creates log files dynamically in tomcat/logs folder (using Routing Appenders). I have written a unit test and have a separate Log4j2 config file for that, but when I run the Junit test, the log4j2 does not creates any file in the tomcat/logs folder. Can you please help with creating the logs from the junit tests?
Thank You!
Deepak
What is current configuration?