import java.util.logging.Logger; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); public void myMethod() { String message = "This is an info message"; LOGGER.info(message); } }
import java.util.logging.Logger; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); public void myMethod() { String message = "This is a debug message"; LOGGER.finest(message); } }
import java.util.logging.Logger; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); public void myMethod() { try { // some code that may throw an exception } catch (Exception e) { LOGGER.severe("An error occurred: " + e.getMessage()); } } }This code logs an error message using the java Logger if an exception is caught. The message includes the error message from the exception. Overall, the java Logger is a powerful tool for logging messages during program execution. It can be configured to output messages to console, files, or other destinations, and can be used to track errors and debug code.