Updated date:

Java Examples: Logging Levels Explained

Author:

I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.

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 1000 log entries to the disk on a specific operation. Out of this 1000 entries, let us say 900 messages are Informational, 85 messages are Warning and 15 messages are Fatal Errors. Now Logging all 1000 message every time is not advisable especially when an application is performing well. The best choice we can think of is, leaving the application to log only Fatal Errors first. And when Fatal Errors are captured during unexpected conditions, we can decide for more capturing. That is, we can ask the application to log all 1000 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 Log Level Works?

The Logging Levels are constant values that are defined in the “Level Class” of the “java.util.logging” package. It defines 7 Constants and those are shown in the below Table:

Logging LevelDescriptionConstant Value

SEVERE

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

1000

WARNING

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.

900

INFO

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

800

CONFIG

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

700

FINE

This is the level used to log developer specific information.

500

FINER

This is the level used to log developer specific information.

400

FINEST

This is the level used to log developer specific information.

300

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 sub-sequent 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
Logr.setLevel(Level.WARNING);

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
Logr.setLevel(Level.INFO);

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

Best Default Logging Level - Polling from Readers

4. Conclusion

In the above example, we had experimented how setLevel() method is used to set the Logger at 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 "logging.properties" file in the "Lib" folder of the JRE location.

To capture log messages whose Logging Levels is lower than the INFO (Say; FINER), we have to use "Handlers" and we will see about that in a separate Article.

Take a Break

For each question, choose the best answer. The answer key is below.

  1. How much log we capture is controlled through “Logging Levels” - True/False
    • True
    • False
  2. WARNING is the Highest “Logging Level” - True/False
    • True
    • False
  3. The default Logging Level of Console Window is “INFO” - True/False
    • True
    • False

Answer Key

  1. True
  2. False
  3. True

© 2018 sirama

Related Articles