Home » Log4j Logging Levels

Log4j Logging Levels

by Online Tutorials Library

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:

  • You can filter your log files during the search.
  • You can manage the amount of information that you log.

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:

Log Level Description
ALL This level turns on all levels of logging. It includes the custom logging levels that you have defined. Once this one is configured and the levels are not considered at all, then all the appenders will start pouring the log events in log files.
DEBUG Debug is used a lot for debugging the application at development time. Every log message will appear to log files once this level is set. It basically belongs to developers.
INFO The INFO logging level is used to record messages about routine application operation. In real-time, system administrators watch the info logs to ensure what’s happening on the system right now, and if there is any problem in normal flow.
WARN WARN log level is used to indicate that you might have a problem and that you’ve detected an unusual situation. Maybe you were demanding to invoke a service, and it failed a couple of times before connecting on an automatic retry. It is unexpected and unusual, but no real harm was done, and it’s not known whether the issue will persist or recur. Someone should investigate warnings.
ERROR The ERROR log level is used to denote a serious problem that you must have to investigate immediately. Not as serious as FATAL, but still a problem. It simply means that your application has met really undesired state. For example, unexpected formatted input, database unavailability.
FATAL The FATAL log level, like ERROR, designates a problem. But unlike ERROR, it designates a very serious error event. You will not consider their presence very much on a normal day, but once they appear, it signals very bad news, even the application of death.
OFF This is the highest possible rank and is intended to turn off logging.
TRACE This has been recently introduced in version 1.2 and includes more information to debug level logs.

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.

Log4J Logging Levels

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

Configuration File:

Java Program:

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!  

You may also like