Log4j Layer Objects Introduction

Log4j is designed in a multiple-layer pattern. So there are multiple layers in it. And each layer has a different object to perform different actions. The below picture is a general view of all objects contained in log4j, we will introduce them one by one.

log4j-objects-architecture

 

Generally, we divide log4j objects into the below two types.

  1. Core Objects: These are the must-have objects that support the framework to run.
  2. Assistance Objects: These objects can provide useful and important features.

1. Core Objects.

From the above diagram, we can see the following objects consist of log4j core objects.

  1. Logger Object: This object’s responsibility is to collect log data. It exists in the logger layer which is the top layer in the architecture.
  2. Appender Object: This object is in charge of writing data to the desired target such as a file, database or console, etc. It exists in the appender layer which is a lower layer in the architecture.
  3. Layout Object: This is an important object which is used to format the data in different style before the data is written to the target by appender object. It is exists in the layout layer. Commonly we should use this object to format the data in a human-readable style.

2. Assistant Objects.

  1. The green rectangle in the diagram are assistant objects which also called support objects.
  2. Level Object: This is used to specify which level and above level log data will be logged to the target. There are 7 predefined level values: OFF, DEBUG, INFO, WARN, ERROR, FATAL, and ALL. The back level log data has a higher priority than the previous one.
  3. Below is the introduction of each log level.
    OFF: No data will be logged.
    DEBUG: All data will be logged because debug is the lowest level.
    INFO: All data except DEBUGlevel data will be logged.
    WARN: All data except DEBUG, INFOlevel data will be logged.
    ERROR: Only ERROR, FATALlevel data will be logged.
    FATAL: Only fatal level data will be logged.
  4. LogManager: This object is used to read the log4j initialization data from log4j configuration file(ie : log4j.properties). It will look up the properties file in the CLASSPATH( system environment variable ) by default.
  5. Filter Object: This object is used with the appender objects as filters. So when the appender object wants to write data to target, it should first pass the data to the assigned filters, and the filters will analyze which data can be published to the target by predefined rules. After all assigned filters check and approve then the appender can publish the data to the target. You can assign multiple filter objects to one appender.
  6. ObjectRenderer: We all know you can log string data like logger.info("hello log4j");. This will output something like below.
    [main] INFO com.dev2qa - hello log4j
    But how about log a java object such as logger.info(new HelloWorld("Jerry", "Welcome to dev2qa.com")); If you do not override the HelloWorld object’s toString() method, then the output will be [main] INFO com.dev2qa - [email protected]. It is not human-readable. So now you can utilize the ObjectRenderer interface to make HelloWorld object render human-readable string when you log it in log4j. We will have another article to show you how to do that.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x