Check if message is loggable

This is an example of how to check if a message is loggable. We are going to use a Logger with logging.Level set to WARNING and then log messages in different levels, in order to check if they are loggable. To do so, we have to:

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;
import java.util.logging.Logger;
import java.util.logging.Level;
 
public class LoggingLevelCheckExample {
	
    public static void main(String[] args) {
  
    	// Create an instance of Logger and set the logging level to Level.WARNING.
  Logger log = Logger.getLogger(LoggingLevelCheckExample.class.getName());
  log.setLevel(Level.WARNING);
 
  // Log INFO level message
  if (log.isLoggable(Level.INFO)) {
log.info("Application Info Message");
  }
 
  // Log WARNING level message when Level.WARNING is loggable.
  if (log.isLoggable(Level.WARNING)) {
log.warning("Application Warning Information");
  }
 
  // Log SEVERE level message when Level.SEVERE is loggable.
  if (log.isLoggable(Level.SEVERE)) {
log.severe("Info Severe Information");
     
  }
    }
}

Output:

Αυγ 12, 2012 2:01:54 ΜΜ com.javacodegeeks.snippets.core.LoggingLevelCheckExample main
WARNING: Application Warning Information
Αυγ 12, 2012 2:01:54 ΜΜ com.javacodegeeks.snippets.core.LoggingLevelCheckExample main
SEVERE: Info Severe Information

 
This was an example of how to check if a message is loggable in Java.

Exit mobile version