Level object of Log4j

Although Log4j’s Level object is not the main component, it is the first object we need to know. This is because the Level object defines the priority or severity of the message content that we need to log. In this tutorial, we will learn more about this Level object!

Levels of Level object

There are 7 levels defined in the Level object:

  • ALL: This is the lowest level, Logger and Appender are defined at this level, all log information is logged.
  • DEBUG: At this level, the debug information will be logged.
  • INFO: At this level, information about the workflow of the program will be logged.
  • WARN: level allows us to log the program’s warning information.
  • ERROR: errors when running the program will be logged if we use this level.
  • FATAL: This level will log serious errors occurring in the program, which can make the program unusable.
  • OFF: This is the highest level, which is used when we do not want to log any more information.

The priority of the low to high levels is as follows: ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.



Filtering information to log based on the Level object

As mentioned above, the Level object defines the priority of the log information. The log information will be filtered based on the priority that Logger and Appender are defining.

For example, the Logger object and the Appender object are defined with a Level of INFO, so all the log level information is INFO, WARN, ERROR, and FATAL.

Consider the example in the previous tutorial, now I will edit the Log4jExample class with the following content:

As you can see, I’m defining 4 Levels: DEBUG, INFO, WARN, ERROR for the Logger object in the Log4jExample class.

Log4j’s Root Logger is defined with priority as INFO, so we get the following result:

If I edit the Root Logger’s priority to ERROR, only the following message is logged:

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment