Configuration file for log4j: An introduction

Reading Time: 3 minutes

Log4j.xml is nothing but configuration file of log4j means you can customize loggers, in this blog we will be looking into this file to now the nuts and bolts of it. Log4j is a java based logging utility used for logging. Logging is an essential add on for any application. It not just printing the messages but also gives you detailed information about the messages. Every one and specially developers must involved in sending information of at least every layer of the architecture. Logs are also useful to detect common mistakes user makes, as well as for security purpose. That means logs are not concise to print only error messaged but also other information. To know more about log4j click here.

Here in this blog we will be discussing log4j.xml file which is to configure log4j. Let’s go through the steps to set your maven project to user log4j loggers then we will come to log4j.xml.

Maven dependency of log4j

<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

After adding maven dependency of log4j you need to add log4j.xml file parallel to src folder. Here is a sample project structure

Logging to console :

Add this to log4j.xml file in order to print loggers on console

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
 
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
  </appender>
 
  <root>
    <priority value ="debug"></priority>
    <appender-ref ref="console"/>
  </root>
 
</log4j:configuration>

Lets us understand all the tags in this file :-

  • appenders – This tag includes where to log, here in the above example we are logging information to the console
  • layout – This tag includes in what pattern we want to produce the information. In this example we have set the pattern with date along with time an then the location
  • priority – This sets the level of logs means what particular type of logs you want to produce, we will be discussing more about this later in this blog
  • root – Whatever level we have set in the priority it will be reflect through to the project level. Yes you are right we can also set log level to the package level or even a class level
  • appender-ref – You may consider it as the answer of how to log. This tag will refer to the appender itself where we have defined how to log.

Log levels

What are log levels

Amount or type of information in the system is controlled by the log levels. As you already know there are more than one type of loggers and say you want to see only trace loggers in all the types. So all you need is to set the level to trace or all.
You may refer here to know more about the log levels.

Why we need log levels

Say you want to get all the information about the warnings in the system or your client asked about only error loggers, there we will not going manually separate the loggers(believe me it will be very tedious). Instead all you need to do is to set an appropriate log level and that’s all.

Hope you guys like this blog post and let me know your view about the loggers in the comment section below.

Reference :-
https://logging.apache.org/log4j/2.x/


Knoldus-blog-footer-image

Written by 

Alok Jha is the QA Consultant at Knoldus Software LLP. He has good knowledge of languages Java, Java 8, Rust and JavaScript. As a QA, he always tries to explore the different type of software and tools.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading