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)

Wayan Saryada

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply