Skip to main content

Java Examples: Logging Levels Explained

  • Author:
  • Updated date:
Read on for a lesson in coding!

Read on for a lesson in coding!

1. Introduction to Logging Levels

All logging that an application writes to a file consumes disk space. This disk space can easily grow in a day or week depending on the volume of captured information.

For example, let us say an application is writing 1,000 log entries to the disk on a specific operation. Out of these 1,000 entries, let us say 900 messages are Informational, 85 messages are Warning and 15 messages are Fatal Errors. Now logging all 1,000 messages every time is not advisable especially when an application is performing well. The best choice we can think of is to leave the application to log only Fatal Errors first. And when Fatal Errors are captured during unexpected conditions, we can decide on more capturing. That is, we can ask the application to log all 1,000 messages to diagnose the Fatal Errors.

The condition of dynamically controlling the capturing level of logs can be achieved through “Logging Levels”. While logging each entry, java expects a logging level. The logging level which is set in the Java Logger helps in filtering the logging request. In this article, we will explore different logging levels.

2. How Do Log Level Works?

The logging levels are constant values that are defined in the “Level Class” of the “java.util.logging” package. It defines seven constants and those are shown in the below table:

Logging Level Constants

Logging LevelDescriptionConstant Value


This is the level used to log critical information like application fatal erors or critical conditions.



This is the level used to log suspect for failure(s). The logged information is not failure but it shows something that may go wrong.



This is the level used to log important information. This is not a fauilure and also not a warning alert. Ex: "User abc logged into the System successfully



This is the level used to log the application configuration settings in which a certain operation is performed.



This is the level used to log developer specific information.



This is the level used to log developer specific information.



This is the level used to log developer specific information.


The logging level presented in the table adheres to a specific order. Say for example, the “SEVERE” is the top most of it. Whenever we turn-on the Logging, and log something, it will always get reported. In Contrary, “FINEST” is the lower level of logging which means, the logging has more tuned developer specific information on a crucial functionality.

While setting the logger at particular level say “INFO”, it will not only log the informational messages but also picks up "WARNING" and "SEVERE" message types. For a committed logger level the logger will also log all higher-level messages in the order. The below picture illustrates this.

Logging Level and Logger

Logging Level and Logger

Let us say the logger is set with the INFO level using "Logger.setLevel()". Then all subsequent log() method calls with info and higher levels are logged. In the above depiction two examples are shown which describes what is logged and what is skipped in relation to logging level of the logger.

Besides the above logging levels, there is two special logging levels called “OFF” and “ALL”. The logging level “OFF” is used to turn off the logging and “ALL” turn on the logging. With logging Level set “ALL”, every call to log() method logs the information with no filtering.

3. Code Example for Logging Level

The default console window can display the SEVERE, WARNING and INFO messages. So, we will write an example which writes all these three kinds of messages. And then, we will explore how the logger filters messages based on the logging level set to it.

The "getLogManager()" will give us application wide LogManager instance. The "getLogger()" call on the LogManager gives a logger instance and we are asking the "Java Runtime" to name logger by making use of the constant GLOBAL_LOGGER_NAME.

//Snippet 02: Get the Log Manager Instance
LogManager lgMan = LogManager.getLogManager();

//Snippet 03: Get Logger from Log Manager
String LoggerName = Logger.GLOBAL_LOGGER_NAME;
Logger Logr = lgMan.getLogger(LoggerName);

After we have logger in hand, we are setting the logging level to the logger. In the below code snippet, we are setting logging level to WARNING. This will allow the logger to log only SEVERE and WARNING messages. All other message types starting from INFO to FINEST will be skipped by the logger.

//Snippet 04: Set the Log Level @ Logger

After setting logging level to the logger, the example is logging different log messages through the logger instance named "Logr". In the below code One SEVERE, Two WARNING and Six INFO messages are logged. Since the logger is set with WARNING, the logger Skips INFO and allows SERVER, WARNING messages.

//Snippet 05: Test Log Entries with Different
//Logging level
//5.1: Log a Fatal Error
Logr.log(Level.SEVERE, "Fatal Error 17: Message" );
//5.2: Log Some Warning Messages
Logr.log(Level.WARNING, "Warning 1: Warning Message");
Logr.log(Level.WARNING, "Warning 2: Warning Message");
//5.3: Log Some Informational Messages
Logr.log(Level.INFO , "Info 1: The Message");
Logr.log(Level.INFO , "Info 2: The Message");
Logr.log(Level.INFO , "Info 3: The Message");
Logr.log(Level.INFO , "Info 4: The Message");
Logr.log(Level.INFO , "Info 5: The Message");
Logr.log(Level.INFO , "Info 6: The Message");

The example will produce the output as shown below:

Java Logger Set with Warning Level

Java Logger Set with Warning Level

In the above output, it is evident that only SEVERE and WARNING log messages are processed by the logger instance. Although the logger was asked to log three kinds of messages, it skipped the INFO log messages. Why? Because the logger is set with WARNING log level.

Now let us change logger’s log level to Info, by change the code as shown below:

//Snippet 04: Set the Log Level @ Logger

Now the logger will allow all three kind of messages that we are logging. Below is the output:

Logger at INFO Logging Level

Logger at INFO Logging Level

4. Wrapping Up

In the above example, we experimented with how setLevel() method is used to set the logger at a certain logging level. In our testing, we changed our example code to set the logging level to INFO. These logging levels should be configured in a Property File so that without compiling the code, one can achieve the desired logging level.

One may wonder why the logger does not display messages lower than the INFO in console window. The Util.Logging has default configuration offered by the Java Runtime. The default handler is ConsoleHandler and default level of logging for that is INFO. This is the reason why the console window is not displaying the messages with level lesser than INFO. To know more configuration about default logging, have a look at the "" file in the "Lib" folder of the JRE location.

To capture log messages whose logging levels are lower than the INFO (Say; FINER), we have to use "Handlers" and we will see about that in a separate article.

© 2018 sirama