Log4J Logging Levels
Logging levels are used to categorize the entries in your log file. But they categorize in a very specific way, i.e., by urgency. The level allows you to separate the following kinds of information:
The amount and type of information given in the system, and event logs are controlled by the log4j level settings in the configuration file. Each log message is prefixed by the level of the message.
The logging levels are an instance of org.apache.log4j.Level class.
Log4j has the following levels of logging:
How to set Log Levels?
Set log level in log4j.properties
Set log level in log4j.xml
How do Log Levels Works?
The working of logging levels is actually very simple. During runtime, the application code will create logging requests, which will have a level. At the same time, the logging framework has a log level configured, which acts as a threshold. If the request level is at the configured level or higher level, it gets logged to the configured target. If not, it's denied. It's simple as that.
Let's consider that as the following rank order for the levels:
So if, for instance, the logging framework level is set to WARN, requests with any of the levels WARN, FATAL, and ERROR will be accepted, while the rest will be denied.
In the above diagram, the vertical header displays the Level of the LogEvent, while the horizontal header shows the Level associated with the appropriate logging configuration.
For the first column, you will see how the log works in each level. For example, for WARN, (FATAL, ERROR, and WARN) will be visible. For OFF, nothing will be visible.
Log4j Level Example
Now compile and run the above program and we would get the following output in c:/usr/home/log4j/log.out file:
Warn Message! Error Message! Fatal Message!