Java Examples: Logging Levels Explained

Updated on June 20, 2018
sirama profile image

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 Level
Constant 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 informations. 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.
Logging Level Constants

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 | Source

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

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 | Source

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 | Source

Best Default Logging Level - Polling from Readers

What is the Logging Level that you set in your deployed application most of the time?

See results

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 "" 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.

Questions & Answers

    © 2018 sirama


      0 of 8192 characters used
      Post Comment

      No comments yet.


      This website uses cookies

      As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

      For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

      Show Details
      HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
      LoginThis is necessary to sign in to the HubPages Service.
      Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
      AkismetThis is used to detect comment spam. (Privacy Policy)
      HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
      HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
      Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
      CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
      Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
      Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
      Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
      Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
      Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
      Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
      VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
      PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
      Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
      MavenThis supports the Maven widget and search functionality. (Privacy Policy)
      Google AdSenseThis is an ad network. (Privacy Policy)
      Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
      Index ExchangeThis is an ad network. (Privacy Policy)
      SovrnThis is an ad network. (Privacy Policy)
      Facebook AdsThis is an ad network. (Privacy Policy)
      Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
      AppNexusThis is an ad network. (Privacy Policy)
      OpenxThis is an ad network. (Privacy Policy)
      Rubicon ProjectThis is an ad network. (Privacy Policy)
      TripleLiftThis is an ad network. (Privacy Policy)
      Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
      Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
      Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
      Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
      ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
      Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
      ClickscoThis is a data management platform studying reader behavior (Privacy Policy)