import java.util.logging.Logger; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); public void myMethod() { LOGGER.log(Level.INFO, "My log message"); } }
import java.io.IOException; import java.io.InputStream; import java.util.logging.LogManager; public class MyClass { private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName()); static { try (InputStream configFile = MyClass.class.getResourceAsStream("/logging.properties")) { LogManager.getLogManager().readConfiguration(configFile); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Error loading logging configuration", e); } } public void myMethod() { LOGGER.log(Level.INFO, "My log message"); } }In this example, we've added a static block that loads a logging configuration file. The configuration file is located at /logging.properties in the classpath. The configuration file specifies a console handler that logs messages at the INFO logging level and a file handler that logs messages at the WARNING logging level. We can then use the logger instance to log messages, which will be handled according to the configuration specified in the file. Package: java.util.logging