copy log4j2.xml in resource folder
create a logger in your class
Here is the class
add these 2 dependencies in POM.xml file
your log file looks like below:)
Let me know what you think in the comment:) Happy coding!
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- Console Appender -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<!-- Rolling File Appender -->
<RollingFile name="RollingFile">
<FileName>C:/Users/Mutturaj/workspace/ExtentReports/log/mylog.log</FileName>
<FilePattern>C:/log/size-based-logs/%d{yyyy-MM-dd-hh}-%i.log.zip</FilePattern>
<PatternLayout>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 KB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="extentReportForMultipleTestsInAClass" level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
create a logger in your class
private static final Logger log=LogManager.getLogger(ClassA.class);
Here is the class
package extentReportForMultipleTestsInAClass;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;
public class ClassA extends BaseTest {
private static final Logger log=LogManager.getLogger(ClassA.class);
@Test
public void method1() {
log.info("Method 1 started");
test = extent.createTest("method1");
driver.get("https://www.facebook.com/");
String title = driver.getTitle();
System.out.println(title);
log.info(title);
Assert.assertEquals(title, "Facebook – log");
}
@Test
public void method2() {
log.info("Method 2 started");
test = extent.createTest("method1");
driver.get("https://www.facebook.com/");
String title = driver.getTitle();
System.out.println(title);
log.info(title);
Assert.assertEquals(title, "Facebook – log");
}
}
add these 2 dependencies in POM.xml file
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
your log file looks like below:)
2019-May-05 11:13:03 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Method 1 started
2019-May-05 11:13:06 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Facebook – log in or sign up
2019-May-05 11:13:12 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Method 2 started
2019-May-05 11:13:15 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Facebook – log in or sign up
2019-May-05 11:15:43 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Method 1 started
2019-May-05 11:15:47 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Facebook – log in or sign up
2019-May-05 11:15:53 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Method 2 started
2019-May-05 11:15:57 AM [main] INFO extentReportForMultipleTestsInAClass.ClassA - Facebook – log in or sign up
2019-May-05 11:16:03 AM [main] INFO extentReportForMultipleTestsInAClass.ClassB - Method 1 started
Let me know what you think in the comment:) Happy coding!
No comments:
Post a Comment