import java.util.logging.*; public class BasicLogging { private static final Logger logger = Logger.getLogger(BasicLogging.class.getName()); public static void main(String[] args) { logger.setLevel(Level.INFO); logger.info("Logging information message."); logger.warning("Logging warning message."); logger.severe("Logging severe message."); } }
import java.util.logging.*; public class CustomFormatting { private static final Logger logger = Logger.getLogger(CustomFormatting.class.getName()); public static void main(String[] args) { logger.setLevel(Level.INFO); ConsoleHandler handler = new ConsoleHandler(); handler.setFormatter(new SimpleFormatter()); logger.addHandler(handler); logger.info("Logging information message."); } }
import java.util.logging.*; public class LoggingExceptions { private static final Logger logger = Logger.getLogger(LoggingExceptions.class.getName()); public static void main(String[] args) { logger.setLevel(Level.SEVERE); try { int result = 1 / 0; } catch (ArithmeticException e) { logger.log(Level.SEVERE, "An arithmetic exception occurred.", e); } } }This example demonstrates how to log exceptions that occur during runtime. It creates an arithmetic exception intentionally, then logs it using the logger's log method. The output of this code will include a stack trace of the exception. In conclusion, Java LOG is a package library provided by Java that allows developers to log important information about their application's execution. It includes a vast range of features, from basic logging to custom formatting, and is used widely for debugging and diagnostics. Java LOG is part of the standard Java packages and does not require any additional libraries or dependencies.