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.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 final 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. In the output below we see that the "This message won't be logged." is not displayed.

Oct 08, 2021 7:12:53 AM org.kodejava.util.logging.LogLevelSetting main
SEVERE: This message will be logged.
Oct 08, 2021 7:12:54 AM org.kodejava.util.logging.LogLevelSetting main
INFO: Information message.
Oct 08, 2021 7:12:54 AM org.kodejava.util.logging.LogLevelSetting main
WARNING: Warning message.
Oct 08, 2021 7:12:54 AM org.kodejava.util.logging.LogLevelSetting main
SEVERE: Severe message.
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.