This Log4J 2 Tutorial comprises log4j log levels and log4j configurations in our java projects. It is created by Apache and in this tutorial, we will add Apache Log4j logging feature to our framework. We will continue our framework which has test listener, Allure reports, Extent Reports, Retry Logic based on the Page Object Model. The latest version which is Log4j 2 is explained in this article. Now, it is time to add a logging capability with log4j 2 to our framework. Let’s start.
What Is Log4j 2?
Log4j 2 is the brand new version of log4j and they are a really common logging framework in many JAVA projects. For sure if the suffix is “2” which means it brings new features and improvements such as asynchronous logging. Also, log4j 1.x is deprecated and no longer maintained that’s why it is better to migrate log4j 2 and I will try to help you for a successful migration. ;)
Log4j 2 Tutorial Project Structure
The project structure is shown in the below screenshot.
Log4J 2 Maven Dependencies
In order to add a logging feature, we should first add the related library to our project by using its maven dependency as follows.
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency>
Log4J 2 Configuration Properties File
Then, we need to add a log4j2.properties file under the resources folder as shown below.
#Declare loggers status = error name= Log4j2PropertiesConfig appenders=a_console, a_rolling rootLogger.level=info rootLogger.appenderRefs = ar_console,ar_rolling rootLogger.appenderRef.ar_console.ref = StdoutAppender rootLogger.appenderRef.ar_rolling.ref= RollingAppender #Console Logger appender.a_console.type = Console appender.a_console.name = StdoutAppender appender.a_console.layout.type = PatternLayout appender.a_console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n #Rolling Logger appender.a_rolling.type = RollingFile appender.a_rolling.name = RollingAppender appender.a_rolling.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n appender.a_rolling.fileName=log4j2/log4j2-test-automation.log appender.a_rolling.filePattern=log4j2-sample-%d{yyyy-MM-dd}.log appender.a_rolling.layout.type = PatternLayout appender.a_rolling.policies.type = Policies appender.a_rolling.policies.time.type = TimeBasedTriggeringPolicy appender.a_rolling.policies.time.interval = 1 # To change log file every day appender.a_rolling.policies.time.modulate = true # To change log file after 10MB size appender.a_rolling.policies.size.type = SizeBasedTriggeringPolicy appender.a_rolling.policies.size.size=10MB appender.a_rolling.strategy.type = DefaultRolloverStrategy appender.a_rolling.strategy.max = 20
Global Logging Manager Class
Now, we need to write a global logging class that is responsible for all kinds of log operations. I created a utility package and under this folder, I created a Log class.
package utils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log { //Initialize Log4j instance private static final Logger Log = LogManager.getLogger(Log.class); //Info Level Logs public static void info (String message) { Log.info(message); } //Warn Level Logs public static void warn (String message) { Log.warn(message); } //Error Level Logs public static void error (String message) { Log.error(message); } //Fatal Level Logs public static void fatal (String message) { Log.fatal(message); } //Debug Level Logs public static void debug (String message) { Log.debug(message); } }
How to use Log4j in Selenium Automation Project
It is time to write logs in our project. First, I added logs when tests are starting and ending as shown below.
Then, we can add logs wherever we want in our project as shown below.
After this, we should run our test and check our project root folder. We should see a log4j folder and under it, we should see our log file. First, I saw the log messages on the console as follows.
The whole test suite’s console output looks like below.
Then, I check the project root folder and I saw the log file. :)
GitHub Project
https://github.com/swtestacademy/TestNGAllureReport/tree/log4j-tutorial
Thanks for reading.
Onur

Onur Baskirt is a Software Engineering Leader with international experience in world-class companies. Now, he is a Software Engineering Lead at Emirates Airlines in Dubai.