How Java uses JUL to record logs

Java logging is a way to record program runtime information provided by the Java Standard library, which can help developers track and debug applications. Java's built-in logging tools include Java Util Logging (JUL), which is the default logging framework for the Java platform. JUL is a flexible and powerful logging framework mainly composed of the following key classes: 1. Logger: Responsible for creating a log logger. Obtain a Logger instance by calling the 'Logger. getLogger (String name)' method. Logger can be organized hierarchically based on its name, making it convenient to record logs separately for different modules. 2. Handler: Responsible for outputting log information to different targets. JUL supports multiple different handlers, such as ConsoleHandler (console output), FileHandler (file output), and can also customize handlers. 3. Formatter: Responsible for formatting log records into strings displayed on the output target. JUL provides a simple log formatter by default and also supports custom formatters. 4. Level: defines different levels of logs, which are divided into SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST according to their severity. Below are some commonly used methods and sample Java code to demonstrate how to use JUL to record logs. 1. Configure JUL: The configuration information of JUL is usually placed in a file called 'logging. properties'. The path to the configuration file can be specified through the 'java. util. logging. config. file' system attribute, or the default configuration can be used. The following is a simple configuration file example: #Output Log Format java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter #Set Log Level java.util.logging.ConsoleHandler.level = INFO 2. Create a logger: import java.util.logging.Logger; public class MyLogger { private static final Logger logger = Logger.getLogger(MyLogger.class.getName()); public void logInfo() { logger.info("This is an information log message"); } } 3. Output to console: import java.util.logging.ConsoleHandler; import java.util.logging.Level; import java.util.logging.Logger; public class ConsoleLoggerExample { private static final Logger logger = Logger.getLogger(ConsoleLoggerExample.class.getName()); public static void main(String[] args) { ConsoleHandler consoleHandler = new ConsoleHandler(); ConsoleHandler. setLevel (Level. ALL)// Set the logging level of the handler Logger. addHandler (consoleHandler)// Add Handler to Logger logger.info("This is an information log message"); logger.warning("This is a warning log message"); } } 4. Output to file: import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; public class FileLoggerExample { private static final Logger logger = Logger.getLogger(FileLoggerExample.class.getName()); public static void main(String[] args) { try { FileHandler fileHandler = new FileHandler("app.log"); FileHandler. setLevel (Level. ALL)// Set the logging level of the handler Logger. addHandler (fileHandler)// Add Handler to Logger logger.info("This is an information log message"); logger.warning("This is a warning log message"); } catch (IOException e) { logger.log(Level.SEVERE, "Failed to create log file", e); } } } The above code example uses JUL to record logs, and the configuration in the example can be customized as needed. For the Handlers and Formatters used in the above example, the corresponding implementations have been provided by default, without the need for additional import dependencies. Tip: JUL is a built-in logging framework in the Java Standard library, so no additional maven dependency is required.