Log4j is designed in multiple layer pattern. So there are multiple layers in it. And each layer has different object to perform different actions. Below picture is a general view of all objects contained in log4j, we will introduce them one by one.
Generally we divide log4j objects into below two type.
- Core Objects : These are the must have objects that support the framework to run.
- Assistance Objects : These objects can provide useful and important features.
1. Core Objects.
From above diagram we can see following objects consist core objects.
- Logger Object : This object’s responsibility is to collect log data. It exist in logger layer which is the top layer in the architecture.
- Appender Object : This object is in charge of writing data to the desired target such as file, database or console etc. It exist in the appender layer which is a lower layer in the architecture.
- 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 exist in layout layer. Commonly we should use this object to format the data in human readable style.
2. Assistant Objects.
The green rectangle in the diagram are assistant objects which also called support objects.
- 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 value: OFF, DEBUG, INFO, WARN,ERROR, FATAL, and ALL. The back level log data has higher priority than the previous one.
off : No data will be logged.
debug : All data will be logged because debug is the lowest level.
info : All data except debug level data will be logged.
warn : All data except debug, info level data will be logged.
error : Only error, fatal level data will be logged.
fatal : Only fatal level data will be logged.
- 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.
- Filter Object : This object is used with appender object as filters. So when appender object want 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 object to one appender.
- Object Renderer : 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 ultilize ObjectRenderer interface to make HelloWorld object render human readable string when you use log it in log4j. We will have another article to show you how to do that.