NashTech Insights

​Introduction to Log4j

krishnajaiswal11
krishnajaiswal11
Table of Contents
background, abstract, line-2462433.jpg

Introduction

Log4j plays a vital role in understanding and debugging the runtime behavior of the programs. Logging means some way to show the state of the system at runtime. Logs are used to catch and persists the important data and makes it accessible for analysis at any point in time.

Log4j2 Configuration

There are various ways to use Log4j2 configuration in our application.

  1. By using a configuration file written in XML, JSON, YAML, or properties file.
  2. Operating Programmatically, we have to construct a configuration factory and implement the configuration.
  3. Using Programmatically, we have to call APIs exposed in the configuration interface
  4. Using Programmatically, we have to call methods on the internal logger class.

Add Log4j to the Project

Log4j2 is the improved version of the Log4j logging framework. The most constraining improvement is the possibility of asynchronous logging. Log4j 2 requires the following libraries:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.6.1</version>
</dependency>

Why to use Log4j?

  • Log4j is an open source.
  • By using log4j, it becomes possible to keep the flow details of our automation in a database or file.
  • Log4j is used in small as well as large projects.
  • We use the log statements rather than (System.out.prinrtln) statements in the code to know the condition of a project while it is executing.

Features

  • log4j is thread-safe.
  • It supports various output appenders for each logger.
  • Logging operation can be placed at runtime by using a configuration file.
  • log4j is based on logger hierarchy.
  • log4j is able to control Java Exceptions from beginning.
  • log4j uses various levels such as ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL.
  • Log4j output can be easily replaced by extending the Layout class.

​Main components

  1. Logger:- Logger is used to log the messages.
  2. Appender:- Appender is used to publish the logging information to the destination like console, database, file etc.
  3. Layout:- The Layout is used for formatting the log information in different styles.

Log4j Properties

The log4j.properties file is a configuration file which accumulate properties in the form of key-value pairs. The log4j properties file consist of the entire runtime configuration used by log4j. This Log4jfile contains the log4j appenders details, log level details and the output file names for file appenders.

  • The level of the root logger is specified as DEBUG, The DEBUG appender is called FILE to it.
  • The appender FILE is represented as org.apache.log4j.FileAppender. It writes to a file called log.out found in the log directory.
  • The layout pattern illustrated is %m%n, which denotes that the printed logging message will be observed by a newline character.

The LogManager always looks for a file “log4j.properties” in CLASSPATH.

​log4j.properties Syntax:

# Define the root logger and the appender.
log4j.rootLogger = DEBUG, X  
  
# We can add the appender named X to be a File appender  
log4j.appender.X=org.apache.log4j.FileAppender  

# Define the layout for X appender  
log4j.appender.X.layout=org.apache.log4j.PatternLayout  
log4j.appender.X.layout.conversionPattern=%m%n.

​Sample Program

This below Java class is a simple example to explain log4j.

import org.apache.log4j.Logger;
import java.io.*;
import java.util.*;

public class Log4jDemoTest{

   static Logger logger = LogManager.getLogger(Log4jDemoTest.class);
   
   public static void main(String[] args)throws IOException{
      log.debug("This is debug log");
      log.info("This is info log");
      log.warn("This is warn log");
      log.error("This is error log");
      log.fatal("This is fatal log");
   }
}

Advantages of Logging

  • Quick Debugging.
  • Problem Diagnosis.
  • Easy Maintenance.
  • Cost and Time Savings.

Conclusion

In this blog, we have demonstrated what is log4j, features of log4j, how to implement log4j and advantages of using log4j.
Reference link:- https://en.wikipedia.org/wiki/Log4j.

krishnajaiswal11

krishnajaiswal11

Suggested Article

%d bloggers like this: