import java.util.logging.FileHandler; import java.util.logging.Logger; import java.util.logging.SimpleFormatter; import java.io.IOException; public class ExampleLogger { private static final Logger logger = Logger.getLogger(ExampleLogger.class.getName()); public static void main(String[] args) { // Create file handler FileHandler handler = null; try { handler = new FileHandler("log.txt"); logger.addHandler(handler); SimpleFormatter formatter = new SimpleFormatter(); handler.setFormatter(formatter); } catch (IOException e) { logger.warning("Failed to create file handler"); } // Log some messages logger.info("Starting application"); logger.warning("Something may go wrong!"); // Close handler if (handler != null) { handler.close(); } } }This example creates a logger object and adds a 'FileHandler' to it. The 'FileHandler' writes the log messages to a file named 'log.txt'. The logging output is formatted using a 'SimpleFormatter'. The logger logs two messages, one at the 'info' level and one at the 'warning' level. Finally, the handler is closed to flush any remaining output and release resources. This code example belongs to the java.util.logging package.