How do I prevent the logger send log messages to its parent logger?

To prevent log records being forwarded to the logger’s parent handlers we can
set false the useParentHandlers field using the
Logger.setUserParentHandlers(boolean useParentHandlers) method.

package org.kodejava.example.util.logging;

import java.util.logging.Logger;
import java.util.logging.ConsoleHandler;

public class NoParentLogger {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(NoParentLogger.class.getName());

        //
        // Do not forward any log messages the the logger parent handlers.
        //
        logger.setUseParentHandlers(false);

        //
        // Specify a ConsoleHanlder for this logger instance.
        //
        logger.addHandler(new ConsoleHandler());
        logger.info("Logging an information message.");
    }
}

How do I set the formatter of logger handlers?

In this example we’ll see how to set the formatter for the logger handlers.
We set the formatter by calling the Handler.setFormatter() method.
In the code below we set a SimpleFormatter for our FileHandler
handler and XMLFormatter for the ConsoleHandler
handler.

This SimpleFormatter format the log in a plain text information
while on the other side the XMLFormatter format the log record in
XML format.

package org.kodejava.example.util.logging;

import java.util.logging.*;
import java.io.IOException;

public class LogFormatter {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(LogFormatter.class.getName());

        try {
            //
            // Create a FileHanlder that will log to mylog.txt with a
            // SimpleFormatter.
            //
            FileHandler simpleHandler = new FileHandler("mylog.txt", true);
            simpleHandler.setFormatter(new SimpleFormatter());
            logger.addHandler(simpleHandler);

            //
            // Create a ConsoleHanlder that will log to the console with
            // an XMLFormatter.
            //
            ConsoleHandler consoleHandler = new ConsoleHandler();
            consoleHandler.setFormatter(new XMLFormatter());
            logger.addHandler(consoleHandler);

            //
            // Do not sent the message to parent handlers. 
            //
            logger.setUseParentHandlers(false);
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Fail to create logger file handler.", e);
        }

        logger.info("Logging application information message.");
        logger.warning("Logging application warning message.");
    }
}

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)