import java.util.logging.Logger; public class MyLogger { private static final Logger logger = Logger.getLogger(MyLogger.class.getName()); public static void main(String[] args) { logger.info("This is a log message"); } }
import java.util.logging.Level; import java.util.logging.Logger; public class MyLogger { private static final Logger logger = Logger.getLogger(MyLogger.class.getName()); public static void main(String[] args) { logger.setLevel(Level.WARNING); logger.info("This is a log message"); logger.warning("This is a warning message"); } }In both examples, the logger object is created using the getLogger method from the Logger class. We can specify the name of the Logger object using the getName method. In example 2, we also set the level of the logger to WARNING so that only warning messages and above are recorded. The package library used in these examples is java.util.logging.