# LOG4J : Explanation & architecture

In 
Published 2022-12-03

This tutorial explains to you what log4j is and its architecture. This is a log4j overview.

Almost every large application includes its own logging mechanism. We need to log information about the application we run, the state of some instances and all the time we need to log errors and some exceptions. These messages generally are divided into the following groups:

  • SEVERE (HIGHEST LEVEL) - sometimes we have ERROR
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (LOWEST LEVEL)

Sometimes we have two extra options: ALL and OFF. ALL causes the Logger to log all messages, while OFF disables logging.

We don't have to write logging mechanism because there some, and we can use/ extend them: LOG4J, Logback, tinilog, etc. These frameworks provide the objects, methods and a configuration files necessary to transmit log messages. Java provides a default framework in the java.util.logging package.

Log4j is a Java library that specializes in logging. Here is the architecture overview of log4j framework:

Here are some good things to know regarding the log4j architecture:

  • The log4j Logger object is responsible for capturing logging information
  • Each Logger is independently configurable
  • The log4j Appender object is responsible for writing (publishing) logging information to various destinations such as a files, databases, JMS, console, UNIX Syslog, etc.
  • A Logger can send log messages to multiple Appenders
  • The log4j Layout Object is used to format logging information (we can get "one-line-at-a-time", XML, HTML, JSON and other formats)
  • The log4j Filters can be defined on configuration files to give more fine-grained control over the logging information (burst filters, time filters for instance). The BurstFilter is a logging filter that regulates logging traffic.
  • The log4j LogManager read the initialization parameters and manage the log4j framework
  • The log4j ObjectRenderer is provides a String representation of different objects passed to the logging framework. This object is used by log4j Layout objects.

In LOG4J there are 5 normal levels of logger:

  • DEBUG : Most useful to debug an application.
  • INFO : It provides informational messages.
  • WARN : It provides that application may have harmful events.
  • ERROR : It provides that application having error events but that might allow it to continue running.
  • FATAL : It denotes the severe error events which make the application to abort.

We can also use:

  • ALL : All the messages are logged
  • OFF : To disable logging in Java