Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Log4j2 Tutorial 4 – Print Logs In External File Using XML Configuration File of Log4J2

Posted on 03/21/2025 By admin

In the previous post, we have learned to configure an XML file to print logs in a console i.e. console appender. But when the execution is over your console log will be gone until you divert it into an external file. Your desired logs will be mixed with other execution logs which you don’t want. So we may need to print logs in a separate file to which we can refer any time.

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 have already learned about the configuration file in Log4j2 and where to place it and how to name it in the previous post. In this post, we will make changes to a configuration file so that the log will be stored in an external file.

Below is a very basic XML configuration to print logs in an external file.


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

We had a console appender i.e Console tag inside the Appender tag in the previous post. In the same way, we need to have a File tag inside the Appender tag. We need to pass some attributes to the File tag i.e name and filename. name is given to File appender which will be used in Logger tag and filename is the file name to be created for store log.

There is no need to change anything in the program.

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

You will see a folder named “log” will be created within the root of the project and a file “myapp.log” will be created. You can see logs as shown below in that file. Since we have set log level as trace so it will print everything.

If we run the same program again and make sure we do not delete the existing log file then you will notice that new logs after another run will be appended in the same file.

17:54:25.033 [main] DEBUG appCode.ModuleA - It is a debug logger.
17:54:25.048 [main] ERROR appCode.ModuleA - It is an error logger.
17:54:25.048 [main] FATAL appCode.ModuleA - It is a fatal logger.
17:54:25.048 [main] INFO  appCode.ModuleA - It is a info logger.
17:54:25.048 [main] TRACE appCode.ModuleA - It is a trace logger.
17:54:25.048 [main] WARN  appCode.ModuleA - It is a warn logger.
09:40:22.805 [main] DEBUG appCode.ModuleA - It is a debug logger.
09:40:22.809 [main] ERROR appCode.ModuleA - It is an error logger.
09:40:22.809 [main] FATAL appCode.ModuleA - It is a fatal logger.
09:40:22.809 [main] INFO  appCode.ModuleA - It is a info logger.
09:40:22.810 [main] TRACE appCode.ModuleA - It is a trace logger.
09:40:22.810 [main] WARN  appCode.ModuleA - It is a warn logger.

We may not require this behavior. To override this behavior we need to set “append” attribute as “false” for File tag as below:-


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

If you run the same program then the log file is overwritten.

We are losing old log data in the above setup which we may not want. We can create a log file with a timestamp which will give the log file a unique name always i.e. at every run.


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

When we execute the same program then a new log file with a current timestamp in its name will be created.

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 72 – How To Compare Part of JSON Objects and Arrays using JSONassert library
Next Post: REST Assured Tutorial 59 – How To Create JsonPath For Simple And Nested JSON Array?

Related Posts

image – Make Selenium Easy Uncategorized
Selenium Framework 3: Types Of Selenium Frameworks Uncategorized
Untitled Diagram (6) – Make Selenium Easy Uncategorized
PartialTextSeelction – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
image – Make Selenium Easy 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