log4j2.xml

LOG4J2:

Log 4j is used to capture the run time logs of the application or our automation framework execution details, We can configure the information we need and that can be shown on console so that it becomes easy to understand the flow or we can start keeping the log information in to a file and can see the details there as well

Since log4j 1.x has vulnerabilities and is no longer supported. We can see it in Maven Central Repository. We need to go with log4j2 .Add log4j core and log4j api into pom.xml as dependencies.




Log4j2 configuration can be done in two ways i.e. using log4j2.xml and log4j2.properties

In this framework I will be using log4j2.xml

Log levels in log4j: Trace < Debug < Info < Warn < Error < Fatal< Off

1.       OFF

2.       FATAL

3.       ERROR

4.       WARN

5.       INFO

6.       DEBUG 

7.       TRACE  

8.       ALL

Out of all these log levels we can get all other till the log level we have used.

If we are saying Debug, then till debug we can produce log for all.

With file and console appenders

Add log4j2.properties to src/test/resources since we are keeping all resources here. Log4j will scan entire project directory to find this configuration file.

Appenders specifies where logs to be generated, in this framework we are sending logs to console as well as to a file with .log extension. So we need to configure Console and File appenders and for file appenders we need to give name and location of the file. We want log 4j to generate logs in a particular pattern so we need to use pattern layout and specify pattern 

To understand log levels lets create a class with a main method and create all log method


In log4j2.xml set root level =FATAL and run the main class





log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="WARN" strict="true">
<Appenders>
<Appender type="Console" name="STDOUT">
<Layout type="PatternLayout"
pattern="%d{HH:mm:ss.SSS} [%t]%-5level %logger{36} - %msg%n"/>
</Appender>
<Appender type="File" name="file" fileName=".\log\app.log">
<Layout type="PatternLayout"
pattern="%d{HH:mm:ss.SSS} [%t]%-5level %logger{36} - %msg%n"/>
</Appender>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="file"/>
<appender-ref ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>

Comments

Popular posts from this blog

extent-config.xml

extent.properties