Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?

 

Typical Steps involved in Creating Log4j Configuration file

  1. Configure the root logger - What to Log - info/debug/error/fatal messages
  2. Configure individual loggers (if needed).
  3. Specify appenders -- Where to Log -- in File/Console, etc
  4. Specify message layout/Pattern -- How to Log - format to log the message

Configuring a Layout

PatternLayout lets you determine how data is extracted from log events and formatted for output based on a conversion pattern. Conversion patterns act as placeholders for data that appears in each log event. 

For example, let’s break down a PatternLayout commonly used in Log4j:

<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>

%d{HH:mm:ss.SSS} formats the date in terms of hours, minutes, seconds, and milliseconds. %t displays the Logger’s current thread. %level displays the severity of the log event. %logger displays the name of the Logger. %m displays the event’s message. Finally, %n adds a new line for the next event. 

=========================================================================

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="basePath">./logs</Property>
</Properties>

<Appenders>
<RollingFile name="File"
fileName="${basePath}/testlogs.log"
filePattern="${basePath}/testlogs-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
<SizeBasedTriggeringPolicy size="1024" />
</RollingFile>


<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.SeleniumPractise" level="error">
<AppenderRef ref="File" />
</Logger>
<Logger name="regressionSuite" level="Debug">
<AppenderRef ref="Console" />
</Logger>
<Root level="trace">
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>

=========================================================================

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file