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
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I clear the current command line in terminal? - February 14, 2019
- How do I generate random alphanumeric strings? - February 7, 2019
- Why do I get ArrayIndexOutOfBoundsException in Java? - January 29, 2019