Learn the necessary knowledge of the "logging API" framework in the Java library
Learn the necessary knowledge of the "Logging API" framework in the Java library
=================================================================
Introduction:
The "Logging API" framework in the Java class library provides a flexible and configurable logging mechanism to record important information of the application in the operation.In this article, we will introduce the logging API and how to use it in the Java program.We will focus on the core concepts of Logging API, log recorder (loggers), log processors, and formatters, and provide relevant code examples and configuration descriptions.
1. Log recorder (Loggers):
The log recorder is one of the core components of the logging API, which is used to record and manage log messages in the application.Each log recorder is associated with a name and organizes through a hierarchical structure.The root log recorder is the top diary recorder of this level structure, and its name is empty.You can use the static method of the logger class to obtain the instance of the log recorder.
Example code:
import java.util.logging.Logger;
public class ExampleLogger {
private static final Logger logger = Logger.getLogger(ExampleLogger.class.getName());
public static void main(String[] args) {
logger.info ("This is an example log message");
}
}
2. log levels:
Logging API defines seven logs, and is: Severe, Warning, Info, Config, Finer, and Finest.Developers can choose the appropriate log level as needed to record the corresponding log messages.For example, if you want to record the detailed log information during the debugging process, you can choose the FINE level.
Example code:
import java.util.logging.Level;
import java.util.logging.Logger;
public class ExampleLogger {
private static final Logger logger = Logger.getLogger(ExampleLogger.class.getName());
public static void main(String[] args) {
logger.setLevel(Level.FINE);
logger.info ("This is an example log message");
logger.fine ("This is a detailed log message");
}
}
3. Log processor (handlers):
The log processor is used to determine the output target of the log message.Logging API provides a variety of built -in processors, such as Consolehandler, Filehandler, etc.You can set one or more processors for each log recorder to decide which goal of the log message to.
Example code:
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ExampleLogger {
private static final Logger logger = Logger.getLogger(ExampleLogger.class.getName());
public static void main(String[] args) {
ConsoleHandler consoleHandler = new ConsoleHandler();
logger.addHandler(consoleHandler);
logger.setLevel(Level.ALL);
consoleHandler.setLevel(Level.ALL);
logger.info ("This is an example log message");
logger.fine ("This is a detailed log message");
}
}
4. Formatters:
The formatter is used to define the output format of the log message.Logging API provides a variety of predetermined formators, such as Simpleformatter, XMLFORMATTER, etc.You can set a formatter for each log processor to determine the format of the log message when the output is output.
Example code:
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class ExampleLogger {
private static final Logger logger = Logger.getLogger(ExampleLogger.class.getName());
public static void main(String[] args) {
ConsoleHandler consoleHandler = new ConsoleHandler();
SimpleFormatter formatter = new SimpleFormatter();
consoleHandler.setFormatter(formatter);
logger.addHandler(consoleHandler);
logger.setLevel(Level.ALL);
consoleHandler.setLevel(Level.ALL);
logger.info ("This is an example log message");
logger.fine ("This is a detailed log message");
}
}
5. Configure file:
Logging API can be flexibly configured by configured files (usually a .properties file).The configuration file defines the attributes of the log recorder, the processor, and the formatomer, and the relationship between them.When the Java program starts, you can load the configuration file and apply the corresponding configuration.
Attribute example:
properties
# Configure the default log level
.level=FINE
# Configure the attribute of Consolehandler
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINE
# Configure the attributes of the formatr
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
Example code:
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogManager;
public class ExampleLogger {
private static final Logger logger = Logger.getLogger(ExampleLogger.class.getName());
public static void main(String[] args) {
try {
LogManager.getLogManager().readConfiguration(ExampleLogger.class.getResourceAsStream("/logging.properties"));
} catch (Exception e) {
e.printStackTrace();
}
logger.info ("This is an example log message");
logger.fine ("This is a detailed log message");
}
}
in conclusion:
By learning the necessary knowledge of the logging API framework in the Java library, we have learned how to use the logging API in the Java program for log records.Mastering the core concept, log recorder, log level, log processor, and formatomators of Logging API are very important for developing efficient and maintainable applications.I hope this article can help you better understand and apply the knowledge of logging API.