Debugging skills of the logging API framework in the Java class library
Debugging skills of the logging API framework in the Java class library
In Java development, logs are a very important tool that helps us position and solve problems in the program.Java's standard library provides a logging API framework that can easily record information such as events, errors and warnings in the program.This article will introduce some skills to debugging using the Logging API framework, and provide some Java code examples to help understand.
1. Configure log output level
The Logging API framework allows us to configure the log output level as needed.For example, we can set the log output level to the lowest level to capture all log information in the program.During the development and commissioning phase, setting the log output level to Finest can easily track the execution process of the program.The following is a sample code fragment, demonstrating how to configure the log output level in Java to Finest:
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) {
logger.setLevel(Level.FINEST);
// ...
}
}
2. Add log record
Adding a log record to the code is very helpful to understand the execution process and the status of the variable.We can use different levels of logging methods of the Logging API framework, such as `Fine (),` Info (), and `warning ()`, etc., select the appropriate method to record the log as needed.The following is a sample code fragment, demonstrating how to use the logging API framework in Java to record the log:
import java.util.logging.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
public static void main(String[] args) {
Logger.info ("Program starts to execute");
// ...
Logger.warning ("Warning: a problem");
// ...
}
}
3. Add contemporary information
In order to better locate the problem, we can add context information to the log record, such as method name, thread name, timestamp, etc.In the Logging API framework, you can use the method of the `Logrecord` class to add context information.The following is a sample code fragment. Demonstrate how to add the context information to the context to the log record in Java:
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.LogRecord;
import java.util.Date;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
public static void main(String[] args) {
String methodName = "main";
String threadName = Thread.currentThread().getName();
LOGRECORD Record = New Logrecord (level.info, "program start execution");
record.setSourceClassName(MyClass.class.getName());
record.setSourceMethodName(methodName);
record.setThreadID(Thread.currentThread().getId());
record.setMillis(new Date().getTime());
record.setLoggerName(logger.getName());
record.setThreadID(Thread.currentThread().getId());
record.setParameters(new Object[] {methodName, threadName});
logger.log(record);
// ...
}
}
4. Use log processors and formators
The Logging API framework provides a variety of log processors and formators. We can use them to enhance the function and readability of logs as needed.We can select the processor and formatter to use the configuration files (such as `logging.properties`) or dynamic configuration in the code.The following is an example of an example configuration file. Demonstrate how to configure the log processor and formatter:
properties
# logging.properties
handlers=java.util.logging.ConsoleHandler
.level=INFO
java.util.logging.ConsoleHandler.level=FINEST
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
In the above example, we use `java.util.logging.consolehandler` as a processor, and configure its output level to find, and the formators are` java.util.logging.simpleformtter`.
Summarize
Using the logging API framework in the Java library can provide us with effective debugging tools.By configured log output levels, add log records, add context information, and use log processors and formators, we can better locate and solve problems in the program.I hope the content of this article can help readers better use the Logging API framework for debugging.
Reference materials:
- [Java Logging Overview](https://docs.oracle.com/en/java/javase/14/core/java-logging-overview.html)
- [java.util.logging.Logger](https://docs.oracle.com/en/java/javase/14/docs/api/java.logging/java/util/logging/Logger.html)
- [Logging in Java](https://www.baeldung.com/java-logging-intro)