Configure Log4j2 for JUnit Tests

Learn some recommended ways to configure Log4j2 – specific to junit testcases and different from used in production.

Log4j2 Config for JUnit

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

Leave a Comment

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

    Reply
  2. 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
  3. 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
  4. thanks this works.
    I had missed setting System.setProperty(“log4j.configurationFile”,”log4j2-testConfig.xml”);

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

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.