Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Log4j2 Tutorial 3 – Setup Console Appender Using XML Configuration File

Posted on 02/19/2025 By admin

In the previous post, we have learned Creating Log4j2 Maven Project With Default Configuration File. When we do not add any configuration file, Log4j2 provides default configuration and prints error and fatal logs on the standard console.

In this post, we will add a user-defined XML configuration file.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Always use the latest version of dependencies. I am using the below version of Log4j2 which is the latest at the time of writing this post.

  org.apache.logging.log4j log4j-core 2.14.1

A configuration file in Log4j2 helps us to set up where to log, how to log, and what to log. A configuration file is used in Log4j2 to specify appenders and loggers mainly. This file is written in XML, JSON, YAML, or properties format.

A very simple XML configuration file (default provided by Log4j2 – You can not find that in a project) will look like below:-


          

There are two major components in any configuration file i.e. XML, JSON, properties or YAML.

  1. Appenders – Where to print logs. We can have multiple appenders.
  2. Loggers – From where to log and what to log

In the above example, you can see that we are using “Console” tag within “Appenders” tag. It represents that I want to print logs in the console. We can have multiple appenders which we will see later. Inside “Console” tag we have another tag “PatternLayout” which defines the pattern of printing logs. We have seen that in a previous post.

Log4j2 follows a search order to look for the configuration files and I think they have explained best on their official website. Below is a copied content from there.

Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML.

  1. Log4j will inspect the “log4j2.configurationFile” system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.
  2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
  3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
  7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  10. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

We know now that Log4j2 will look for a configuration file in the classpath of projects. For this we should keep a configuration file under the resource folder. We will make changes in the above XML file to print all levels of logs in the console. Under the “Loggers” tag we have another tag called “Root”. Root Logger is the topmost element in every Logger Hierarchy. This “Root” tag contains a property called “level”. We can provide it to any level and we know what are logs will be eligible for printing. I have changed level to “trace” so that all levels will be printed.

Now run the same class from the previous post. There is no need to change anything in class.

package appCode;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ModuleA {
        
        // Creating a logger
        private static Logger logger = LogManager.getLogger();

        // Log messages
        public static void main(String[] args) {
                
                logger.debug("It is a debug logger.");
                logger.error("It is an error logger.");
                logger.fatal("It is a fatal logger.");
                logger.info("It is a info logger.");
                logger.trace("It is a trace logger.");
                logger.warn("It is a warn logger.");

        }
}
14:25:40.153 [main] DEBUG appCode.ModuleA - It is a debug logger.
14:25:40.172 [main] ERROR appCode.ModuleA - It is an error logger.
14:25:40.172 [main] FATAL appCode.ModuleA - It is a fatal logger.
14:25:40.172 [main] INFO  appCode.ModuleA - It is a info logger.
14:25:40.172 [main] TRACE appCode.ModuleA - It is a trace logger.
14:25:40.172 [main] WARN  appCode.ModuleA - It is a warn logger.

You can also change the Pattern layout and there are many patterns available which are given here.

Log4j2 Example Codes

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: REST Assured Tutorial 13 – How to write API Response in a JSON File
Next Post: Git Tutorial 1 – What is Version Control System?

Related Posts

TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG | Make Selenium Easy Uncategorized
interview java programs Uncategorized
Javafaker API – numerify(), letterify() and bothify() methods of Faker Class Uncategorized
Selenium Topics – Page 12 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
REST Assured Tutorial 38 – How Getter & Setter methods Matter For Serialization Deserialization Using POJO Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark