How do I compare Logger Level severity?

In this example we see how we can compare the Level severity between two levels. The Level class has an intValue() method that return the integer value of Level‘s severity.

package org.kodejava.example.util.logging;

import java.util.logging.Level;

public class LogLevelSeverityCompare {
    public static void main(String[] args) {
        Level info = Level.INFO;
        Level warning = Level.WARNING;
        Level finest = Level.FINEST;

        // To compare the Level severity we compare the intValue of the Level.
        // Each level is assigned a unique integer value as the severity
        // weight of the level.
        if (info.intValue() < warning.intValue()) {
            System.out.println(info + "(" + info.intValue() + ") is less severe than " +
                    warning + "(" + warning.intValue() + ")");
        }

        if (finest.intValue() < info.intValue()) {
            System.out.println(finest + "(" + finest.intValue() + ") is less severe than " +
                    info + "(" + info.intValue()+ ")");
        }
    }
}

When we run the program above will see the following result:

INFO(800) is less severe than WARNING(900)
FINEST(300) is less severe than INFO(800)

How do I get the current Level of a Logger?

Here we demonstrate how to obtain the current level of the Logger instance. When the level is not set explicitly the level will be inherited from the parent logger.

package org.kodejava.example.util.logging;

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

public class LoggerGetLevel {
    private static Logger logger = Logger.getLogger(LoggerGetLevel.class.getName());

    public static void main(String[] args) {
        LoggerGetLevel demo = new LoggerGetLevel();
        System.out.println("demo.getLevel(logger) = " + demo.getLevel(logger));

        logger.setLevel(Level.WARNING);
        System.out.println("demo.getLevel(logger) = " + demo.getLevel(logger));
    }

    public Level getLevel(Logger logger) {
        Level level = logger.getLevel();
        if (level == null && logger.getParent() != null) {
            level = logger.getParent().getLevel();
        }
        return level;
    }
}

How do I set the Logger log level?

In this example you see how we can change or set the Logger log level. The log level will tell the Logger which particular log message will be logged.

Logger will only record the log message if the level is equal or higher than the Logger level. For instance when the level set to Level.SEVERE, no message other than message logged with Logger.severe(String message) of Logger.log(Level level, String message) will be logged.

package org.kodejava.example.util.logging;

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

public class LogLevelSetting {
    // Obtains a Logger instance, it will create one if it is not already exist.
    private static Logger logger = Logger.getLogger(LogLevelSetting.class.getName());

    public static void main(String[] args) {
        // Set the log level to Level.INFO, the severe message will be logged.
        logger.setLevel(Level.INFO);
        logger.severe("This message will be logged.");

        // Set the log level to Level.SEVERE, the warning message will not be
        // logged as Level.WARNING is smaller than Level.SEVERE
        logger.setLevel(Level.SEVERE);
        logger.warning("This message won't be logged.");

        // Turn of the log, no message will be logged.
        logger.setLevel(Level.OFF);
        logger.info("All log is turned off.");

        // Turn the logger back on, this will result all the corresponding
        // logger message below will be logged.
        logger.setLevel(Level.ALL);
        logger.info("Information message.");
        logger.warning("Warning message.");
        logger.severe("Severe message.");
    }
}

If we run the program above will see the following result displayed:

29 Apr 09 10:06:50 org.kodejava.example.util.logging.LogLevelSetting main
SEVERE: This message will be logged.
29 Apr 09 10:06:50 org.kodejava.example.util.logging.LogLevelSetting main
INFO: Information message.
29 Apr 09 10:06:50 org.kodejava.example.util.logging.LogLevelSetting main
WARNING: Warning message.
29 Apr 09 10:06:50 org.kodejava.example.util.logging.LogLevelSetting main
SEVERE: Severe message.

In the result above we see that the "This message won't be logged." is not displayed.

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

04 Dec 18 13:49:58 org.kodejava.example.util.logging.LoggingLevelCheck main WARNING: Application Warning Information
04 Dec 18 13:49:58 org.kodejava.example.util.logging.LoggingLevelCheck main SEVERE: Application Severe Information