Home » Core Java » util » logging » Check if message is loggable

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

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:

  • Create an instance of Logger, with the getLogger(String name) API method, using as parameter the name of Class that uses the Logger.
  • Set the logging level to Level.WARNING, with the setLevel(Level newLevel) API method of the Logger. In this case logging at WARNING and all higher levels is enabled.
  • Use the isLoggable(Level level) API method of the Logger for different levels, such as INFO, WARNING, SEVERE, and use the corresponding methods of the logger, info(String msg), warning(String msg), severe(String msg) to log a message in different levels. Since the level is set to WARNING in this example, only the messages that are logged to WARNING and above will be logged.

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of