What is Log4j?
Log4j is a Java library,which is used in logging.  Log4j came into existence in 1996, as an initiative by SEMPER group to create a tracing API. Log4j is an open source tool and licensed under Apache Software License.

introduction log4j
log4j


At its most basic level, we can imagine this as a replacement for System.out.println(sysout) statement in our code.But it is far more superior than sysout,but how? Let’s elaborate on this.

Why Log4j?
Lets discuss some advantages of log4j:-

  • The output from Log4j can go to the console, to an email server, a database table, a log file, or numerous other destination, unlike sysout which outputs to standard output, which typically is a console window.
  • It allows different levels of logging like TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. Say if we set a  particular log level, messages will get logged for that level and all levels above it, For instance, for log level = Error, we will have log messages that are errors and fatal and if we have log level = Info, we will have log messages that are info, warn, error, and fatal.
  • Log4j provides the feature to define the format of output logs. Furthermore we can configure Log4j via a configuration file, making it easy to control the logging behavior by editing this file, without touching the application binary.


Main components of Log4j:-

  1. Logger 
  2. Appender 
  3. Layout

LOGGER:
Logger is responsible for handling the majority of log operations.This object of logger class is responsible for capturing logging information and Control over which logging statements are enabled or disabled.

APPENDER:
Appender is responsible for controlling the output of log operations.This Appender object is responsible for publishing logging information to various preferred destinations such as a database, file, console, etc.

Some example of appender:-

appender log4j
Appenders in log4j



LAYOUT:
Layout is responsible for formatting the output for Appender.
Layout objects play an important role in publishing logging information in a way that is human-readable and reusable. The most popular layouts are the PatternLayout and HTMLLayout.

Don’t worry will have working codes with more explanation about them.


Follow the below readings to learn more because “the best is yet to come…”:-
Downloading log4j 
First test case using Log4j
Using Configuration file in log4j

Leave a Reply

Your email address will not be published. Required fields are marked *