Home » Log4j Architecture

Log4j Architecture

by Online Tutorials Library

Log4j Architecture

Log4j follows a layered architecture where each layer is used to provide different objects to perform different tasks. This layered architecture makes the design easy and flexible to extend in the future.

There are two types of objects available in the log4j framework:

Core objects: Core objects are mandatory objects of the framework. All objects are required to use the framework.

Support Objects: Support objects are optional objects of the framework. They used to support core objects to perform additional but important tasks.

Core Objects

There are following types of core objects or following are the log4J components:

Logger:

The Logger is the top-level layer which provides the Logger object. The Logger object is responsible for taking logging information, and they are stored in a namespace hierarchy.

  • Logger is a class, which is available in org.apache.log4j.*
  • We have to create Logger object one per java class
  • Logger component is used to enable the log4j in our java class
  • Logger methods are used to generate log statements in a java class instead of SOPLS.
  • In order to get an object of Logger class, we need to call a static factory method which will give an object as a return type
  • We have to create a Logger object right after our class name. For example:

Getting logger object:

Note: When you are creating a logger object, we need to pass either fully qualified class name or class object as a parameter where class means current class for which we are going to use log4j.

Logger object has some methods; these methods are used to print the status of our application:

These methods are:

  • debug()
  • info()
  • warn()
  • error()
  • fatal()

These all methods are approximately the same. Priority order of these methods is: debug < info < warn < error < fatal.

Appender:

The appender is the lower layer component, which provides Appender objects. The Appender object is responsible for publishing logging information to various preferred destinations such as a file, database, console, Unix Syslog, etc.

  • Logger classes are used to generate statements in different levels, and Appender takes these logs and stores in some database or files.
  • Appender is not a class; it is an interface.

In log4j, we have different Appender implementation classes:

FileAppender: used to append log events to a file. It supports two more appender classes:

  • RollingFileAppender: Extends FileAppender class to back up the log files when they reach a certain size.
  • DailyRollingFileAppender: Extends FileAppender class so that the underlying file is rolled over at a user-chosen frequency.

ConsoleAppender: Appends log events to System.err or System.out using a layout specified by the user. The default console is System.out.

JDBCAppender: Used for Database.

SMTPAppender: Used to send an email when a specific logging event occurs, typically on errors or fatal errors.

SocketAppender: Used for remote storage.

SyslogAppender: Sends messages to a remote Syslog domain.

TelnetAppender: Specializes in writing to a read-only socket.

WriterAppender: Used to append log events to a Writer or an OutputStream depending on the user’s choice.

Layout:

The layout layer provides Layout objects which are used to format logging information in different styles. It is used to provide support to appender objects before publishing logging information.

Layout objects play an important role in publishing logging information in a way, i.e., human-readable and reusable.

The layout component defines the format in which the log statements are written into the destination repository by the appender.

There are different types of layout classes in log4j:

  • SimpleLayout: It is used to format the output in a very simple manner; it prints the Level, then a dash “-” and then the log message.
  • PatternLayout: Used to format the output based on conversion pattern specified or if none is specified, the default conversion pattern is considered.
  • HTMLLayout: It formats the output as an HTML table.
  • XMLLayout

Log4j Architecture

Support Objects

There are other different objects in the log4j framework that play a vital role in the logging framework:

Level Object: The level object defines the priority and granularity of any logging information. There are seven levels of logging defined within the API: OFF, DEBUG, INFO, ERROR, WARN, FATAL, and ALL.

Filter Object: This object analyzes logging information and makes additional decisions on whether that information should be logged or not.

ObjectRenderer: The ObjectRenderer object is specialized in providing a String representation of different objects passed to the logging framework. This object is used by the Layout object to prepare the final logging information.

LogManager: The LogManager object is used to manage the logging framework. It is used for reading the initial configuration parameters from a system?wide configuration file or a configuration class.


Next TopicLog4j Example

You may also like