Guide to use the ‘logging API’ framework in the Java class library

Guide to use the ‘logging API’ framework in the Java class library introduction: In Java applications, recording logs are a very important task.It can help us diagnose and debug the application problems, record key information of the application when running, and track the behavior of the application.The Java library provides a very powerful and flexible log record framework, namely the 'Logging API' framework.This article will introduce the guidelines for the use of the 'Logging API' framework in detail, and provide a complete programming code and related configuration description. 1. Logging API Introduction: The 'Logging API' framework is a logging framework in the Java class library, which provides a set of classes and methods for recording and managing application logs.It can help developers separate log records from other parts of the application and provide flexible configuration options.Using the 'Logging API' framework, the log can be output to different targets (such as console, files, databases), and filtering and formatting as needed. 2. Basic concept of logging API: Before using the 'Logging API' framework, we need to understand some basic concepts. -Logger: Logger is the core concept in Logging API, which is used to record log messages.Each logger has a name associated with it and can have one or more father Logger.Through Logger, we can perform logging, filtration and formatting operations. -Handler: Handler is used to define the goals of log messages.You can configure multiple processors, and each processor can send log messages to different goals.For example, the console processor outputs the log message to the console, the file processor writes the log message into the file, and the database processor stores the log message into the database. -LEVEL: Level is used to define the importance or priority of log message.Logging API defines a series of levels, including SEVERE, Warning, Info, Config, Fine, etc.By specified log levels, log messages below this level can be filtered. -Formatter (Format): Formatter is used to define the format of log messages.You can use a predetermined formatr or custom formatomer. 3. Steps to record logs with logging API: Below is the basic step of using the 'Logging API' framework to record the log: Step 1: Create a logger object: Create a logger object in the code, you can use Java.util. Logging.logger class to getlogger ().Specify a unique name for the Logger object, and usually uses a complete limited name of the current class as the name. import java.util.logging.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class.getName()); // ... } Step 2: Configure Handler: Configure Handler to define the goal of log message.You can use different types of processors, such as Consolehandler (console processor), Filehandler (file processor), DataBaseHandler (database processor), etc.The following examples output log messages to the console: import java.util.logging.ConsoleHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class.getName()); public static void main(String[] args) { Handler handler = new ConsoleHandler(); handler.setLevel(Level.ALL); logger.addHandler(handler); logger.info ("Logging Example"); } } Step 3: Record log: Use the logger object to record log messages.You can choose different log levels as needed, such as Info, Warning, Severe, etc.The following example records a log -level log message: import java.util.logging.ConsoleHandler; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class.getName()); public static void main(String[] args) { Handler handler = new ConsoleHandler(); handler.setLevel(Level.ALL); logger.addHandler(handler); logger.info ("Logging Example"); } } Step 4: Configure log level and formatting: The log level and formatter can be set by calling the Logger object.The following examples set the log level to Fine, and add custom formators to the log message: import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class.getName()); public static void main(String[] args) { Handler handler = new ConsoleHandler(); handler.setLevel(Level.ALL); handler.setFormatter(new CustomFormatter()); logger.addHandler(handler); logger.setLevel(Level.FINE); logger.fine ("Custom Format Example"); } private static class CustomFormatter extends Formatter { @Override public String format(LogRecord record) { return record.getLevel() + ": " + record.getMessage() + " "; } } } 4. logging file (logging.properties): In addition to the configuration in the code, you can also use a log configuration file for global configuration.Create a file called 'logging.properties' in the classpath of the Java application and specify the required configuration in it.Here are some commonly used configuration examples: -D define handler and related attributes: properties handlers = java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level = ALL java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter -Set the default log level: properties .level = ALL -Set the log level of a specific package or class: properties com.example.package.level = INFO After adding these configurations to the 'logging.properties' file, the Java application will automatically load and apply these configurations. in conclusion: This article introduces the guidelines for the use of the 'Logging API' framework in the Java class library.We understand the basic concept of 'Logging API', and describe the steps of using the logging log in detail.By using objects such as Logger, Handler, Level, and Formatter, we can flexibly configure and manage log messages.In addition, we also introduced how to use log configuration files for global configuration.By following the guidelines provided in this article, developers can better use the powerful 'Logging API' framework in the Java class library to record and manage the logs of the application.