How do I check if a message is loggable?

The Logger class provide a setLevel() method to set the logging level. By setting logging to a certain level we can control which message will be logged.

To determine or check if a message will be logged we can use the Logger.isLoggable(Level level) method. Let see the example below.

package org.kodejava.util.logging;

import java.util.logging.Logger;
import java.util.logging.Level;

public class LoggingLevelCheck {
    public static void main(String[] args) {
        // Obtains an instance of Logger and set the logging level to 
        // Level.WARNING.
        Logger log = Logger.getLogger(LoggingLevelCheck.class.getName());
        log.setLevel(Level.WARNING);

        // Log INFO level message. This message will not be logged due to
        // the log level set to Level.WARNING
        if (log.isLoggable(Level.INFO)) {
            log.info("Application Information 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("Information Severe Information");
        }
    }
}

This will result only the Level.WARNING and Level.SEVERE messages are logged.

Oct 07, 2021 8:18:03 PM org.kodejava.util.logging.LoggingLevelCheck main
WARNING: Application Warning Information
Oct 07, 2021 8:18:03 PM org.kodejava.util.logging.LoggingLevelCheck main
SEVERE: Information Severe Information

How do I obtain or create a Logger?

Since JDK 1.4 a logging API was introduced into the Java class libraries. This API enables our application to logs some messages to record our application activities.

To create an instance of Logger we can call the Logger.getLogger() factory method which will return the available logger for the given namespace, or it will create a new one when it doesn’t exist.

package org.kodejava.util.logging;

import java.util.logging.Logger;

public class LoggingDemo {
    public static void main(String[] args) {
        // Obtaining an instance of Logger. This will create a new Logger
        // is it doesn't exist.
        Logger log = Logger.getLogger(LoggingDemo.class.getName());

        // Log some message using a different type of severity level.
        log.info("Info Message");
        log.warning("Warning Message");
        log.severe("Severe Message");
        log.config("Config Message");
        log.fine("Fine Message");
        log.finer("Finer Message");
        log.finest("Finest Message");
    }
}

After we create the Logger instance we can create a message log by calling the logging method such as info(String message), warning(String message) and severe(String message). Below are some message produces by the Logger.

Oct 07, 2021 8:13:48 PM org.kodejava.util.logging.LoggingDemo main
INFO: Info Message
Oct 07, 2021 8:13:48 PM org.kodejava.util.logging.LoggingDemo main
WARNING: Warning Message
Oct 07, 2021 8:13:48 PM org.kodejava.util.logging.LoggingDemo main
SEVERE: Severe Message