1. At first need to add necessary jar files to project. Then create a log4j2.xml file in your project’s src/test/resources folder. The file appender is defined with a file name of Logs/logs.log, which will write log statements to the logs.log file in a Logs directory relative to the project root.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
    <File name="File" fileName="Logs/logs.log">
      <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
    <Root level="info">
      <AppenderRef ref="Console" />
      <AppenderRef ref="File" />

2.Initialize the Log4j logger in your Java code.

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

public class log4jDemo {
	private static final Logger logger = LogManager.getLogger(log4jDemo.class);
	public static void main(String[] args) {
		logger.debug("Debug log message");
		logger.info("Info log message");
		logger.warn("Warning log message");


By toihid