HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Log4j2 / Configure Log4j2 for JUnit Testcases

Configure Log4j2 for JUnit Testcases

Using different log4j2 configuration file for junit testing is desired by most of the developers. Let’s learn some recommended ways to configure Log4j2 – specific to junit testcases and different from used in production.

Table of Contents

Place log4j2-test.xml in test folder
Use log4j.configurationFile property in @BeforeClass

Place log4j2-test.xml in test 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.

Log4j2 Config for JUnit
Log4j2 Config for JUnit

Use log4j.configurationFile property in @BeforeClass

Another way to introduce different log file for junit tests is – to set log4j.configurationFile property in @BeforeClass annotation in any test class.

E.g. Create test specific logging configuration file log4j2-testConfig.xml and place it in resources 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()
	{
		LOGGER.debug("Debug Message Logged !!!");
        LOGGER.info("Info Message Logged !!!");
        LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));
	}
}

Drop me your questions related to log4j2 configuration for junit tests in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Niranjan

    May 29, 2018

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

  2. Deepak Khobragade

    June 23, 2017

    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

    • Lokesh Gupta

      June 23, 2017

      What is current configuration?

Comments are closed on this article!

Search Tutorials

Log4j2 Tutorial

  • Log4j2 – Introduction
  • Log4j2 – JSON Config
  • Log4j2 – Properties Config
  • Log4j2 – XML Config
  • Log4j2 – ConsoleAppender
  • Log4j2 – RollingFileAppender
  • Log4j2 – Multiple appenders
  • Log4j2 – LevelRangeFilter
  • Log4j2 – HTMLLayout
  • Log4j2 – Fish Tagging
  • Log4j2 – Conversion Patterns
  • Log4j2 – JUnit

Log4j Tutorial

  • Log4j – Introduction
  • Log4j – Properties Config
  • Log4j – XML Config
  • Log4j – Maven Config
  • Log4j – Logging Levels
  • Log4j – ConsoleAppender
  • Log4j – RollingFileAppender
  • Log4j – SocketAppender
  • Log4j – JDBCAppender
  • Log4j – XMLLayout
  • Log4j – HTMLLayout
  • Log4j – Runtime Reload
  • Log4j vs. SLF4j
  • Log4j – RESTEasy + Tomcat 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)