Use the "Utilities Logging" framework in the Java Library for error debugging and failure recipes
Use the "Utilities Logging" framework in the Java Library for error debugging and failure to exclude errors
In the process of software development, error debugging and failure exclusion are very important links.The "Utilities Logging" framework in the Java library is a powerful tool that helps us to locate and solve errors and faults in the application.
The "Utilities Logging" framework in the Java library provides a flexible and scalable log record mechanism.By using this framework, we can record various events and information of the application as needed.Here are some common techniques for using the "Utilities Logging" framework for error debugging and failure exclusion:
1. Configure the log recorder (Logger): First, we need to configure the log recorder.This can be completed by setting some configuration parameters in the Java property file.For example, we can specify the output format of the recorder, the detailed level of the record, etc.The content of the configuration file is as follows:
# Configuration log recorder
handlers=java.util.logging.ConsoleHandler
.level=ALL
java.util.logging.ConsoleHandler.level=ALL
2. Get the log recorder: In the class that needs to record the log, we need to get a log recorder.This can be implemented by using the `logger.getLogger () method.For example, suppose we need to record the log in the class named `myclass`:
import java.util.logging.Logger;
public class MyClass {
private static final Logger logger = Logger.getLogger(MyClass.class.getName());
// ...
}
3. Record log message: Once we get the log recorder, we can use it to record the log message.The `Logger` class provides multiple methods to record different levels of messages, such as` Info (), `,` warning (), `severt ()`, etc.Here are some examples:
Logger.info ("This is an information message."););
logger.warning ("This is a warning message.");
Logger.severe ("This is a serious news.");
We can also add the value of the variable to the log message by using a replacement string:
String name = "John";
int age = 30;
Logger.info (String.Format (" %S is %d.", name, Age);
4. Specify logging level: In the configuration file, we can specify the record level of a specific recorder.Only when the news that reaches the specified level will be recorded.For example, we can set the record level to `Info`, so that only the news of the` Info` and the above level will be recorded:
my.package.MyClass.level=INFO
Or, we can also use the `setlevel () method on the recorder object to dynamically set the record level:
logger.setLevel(Level.INFO);
5. Processing log message: By configured the processor (handler), we can specify the output position of the log message.The Java class library provides multiple processors, such as `Consolehandler` (output to the console),` Filehandler`We can specify the processor to be used in the configuration file:
handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
java.util.logging.FileHandler.pattern=mylog.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.append=true
...
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.FileHandler.level=ALL
In the above configuration example, we used the `Consolehandler` and Filehandler` to output the log message to the console and files, respectively.
6. Custom log format: We can also customize the format of the log message.Specify a custom log format by setting `javava.util.logging.consolehandler.Formatter` parameters.For example:
java.util.logging.ConsoleHandler.formatter=my.package.CustomFormatter
In the customized `CustomFormatter`, we can rewrite the` format () method to realize the logic of its own log format.
Using the "Utilities Logging" framework in the Java library, we can easily perform error debugging and failure.By recording log messages and configuration corresponding processors, we can quickly position and solve problems in the application.At the same time, by setting the record level and custom log format, we can flexibly control the details and output methods of the log records.