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.
There are following types of core objects or following are the log4J components:
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.
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:
These all methods are approximately the same. Priority order of these methods is: debug < info < warn < error < fatal.
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.
In log4j, we have different Appender implementation classes:
FileAppender: used to append log events to a file. It supports two more appender classes:
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.
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:
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.