import java.util.logging.Logger; public class MyProgram { private static final Logger logger = Logger.getLogger(MyProgram.class.getName()); public static void main(String[] args) { logger.warning("Something went wrong!"); } }
import java.util.logging.Filter; import java.util.logging.Level; import java.util.logging.Logger; import java.util.logging.LogRecord; public class MyProgram { private static final Logger logger = Logger.getLogger(MyProgram.class.getName()); public static void main(String[] args) { logger.setFilter(new Filter() { public boolean isLoggable(LogRecord record) { return record.getLevel() == Level.WARNING; } }); logger.info("This message will not appear in the log file"); logger.warning("Something went wrong!"); } }This code creates a filter that only logs messages with a severity level of WARNING or higher. It then sets this filter on the logger instance and writes an info message (which is below the threshold) and a warning message to the console. Package library: java.util.logging Overall, the java.util.logging package is an invaluable tool for debugging and monitoring Java applications. It provides a highly customizable and extensible framework for capturing and filtering log messages, which can help developers identify and fix issues more quickly.