import java.util.logging.*; public class LoggerExample { public static void main(String[] args) { Logger.getGlobal().log(Level.INFO, "Hello World!"); } }
import java.util.logging.*; public class LoggerExample { private static final Logger logger = Logger.getLogger(LoggerExample.class.getName()); public static void main(String[] args) { ConsoleHandler consoleHandler = new ConsoleHandler(); consoleHandler.setLevel(Level.SEVERE); logger.addHandler(consoleHandler); logger.setLevel(Level.FINE); logger.severe("This is a severe message"); logger.fine("This is a fine message"); } }In this example, we create an instance of the ConsoleHandler and set its logging level to SEVERE. We then add the console handler to the logger instance and set the logger's logging level to FINE. When we log the SEVERE message, it will be output to the console because the ConsoleHandler's level is set to SEVERE. The FINE message, however, will not be output because the logger's level is set to FINE, which is lower than the ConsoleHandler's level. Overall, the Logger package is a powerful tool for logging messages in Java programs and can be customized to fit many different use cases.