Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Log4j2 Tutorial 5 – XML Configuration File to Log Into File and Console Using Log4j2 Together

Posted on 03/21/2025 By admin

We have already learnt the basics but important concepts of Log4j2 in series of tutorials in Log4j2 Tutorials. Now we know to create an XML configuration file to print logs in to a File and on to the console.

But sometimes it is good to print logs in to console and file as well. So now we will configure an XML document that will be capable to print logs into a file and on a console as well.

If you are thinking it will be complex then you are not correct. It will be just a combination of above mentioned two posts.

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

We know that to print in to console we need to use console appender and File appender to print logs in to a file. We can use both into same configuration file as below:-


    %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n             

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.");
                
        }
}

When we run above program then we can see logs printed in console and file both as below:-

2021-04-13 07:41:54,448 [main] DEBUG: It is a debug logger.
2021-04-13 07:41:54,451 [main] ERROR: It is an error logger.
2021-04-13 07:41:54,453 [main] FATAL: It is a fatal logger.
2021-04-13 07:41:54,454 [main] INFO : It is a info logger.
2021-04-13 07:41:54,454 [main] TRACE: It is a trace logger.
2021-04-13 07:41:54,455 [main] WARN : It is a warn logger.

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: api testing
Next Post: amod

Related Posts

Selenium Interview Questions Uncategorized
ThreadLocal Static WebDriver For Parallel Execution Uncategorized
How To Install Java 8 On Windows 10 Uncategorized
Conditional5 – Make Selenium Easy Uncategorized
FREQUENTLY ASKED JAVA PROGRAMS 35 – Java Program to Reverse a String Using Stack Uncategorized
Protractor Tutorial 1 – What is AngularJS and Angular Applications? 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