Configure Log4j2 for JUnit Tests

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.

Log4j2 Config for JUnit
Log4j2 Config for JUnit

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

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

7 thoughts on “Configure Log4j2 for JUnit Tests”

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

    
    00:04:43  [main] INFO org.apache.maven.plugin.surefire.SurefirePlugin - 
    00:04:43  [main] INFO org.apache.maven.plugin.surefire.SurefirePlugin - -------------------------------------------------------
    00:04:43  [main] INFO org.apache.maven.plugin.surefire.SurefirePlugin -  T E S T S
    00:04:43  [main] INFO org.apache.maven.plugin.surefire.SurefirePlugin - -------------------------------------------------------
    00:04:43  [ThreadedStreamConsumer] INFO org.apache.maven.plugin.surefire.SurefirePlugin - 
    Running ***************************************************
    ******************************************************************
    00:04:44                             
    2022-01-05 12:34:44,545 main ERROR FileManager (/opt/tomcat/logs/log4j2.log) 
    java.io.IOException: Could not create directory /opt/tomcat/logs java.io.IOException: Could not create directory /opt/tomcat/logs
    00:04:44  	at org.apache.logging.log4j.core.util.FileUtils.mkdir(FileUtils.java:120)
    00:04:44  	at org.apache.logging.log4j.core.util.FileUtils.makeParentDirs(FileUtils.java:137)
    Reply
    • 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.

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

    Reply
  3. thanks this works.
    I had missed setting System.setProperty(“log4j.configurationFile”,”log4j2-testConfig.xml”);

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

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.