The log API provided by JDK is officially released in jdk1.4. Java logging can be used for application, JSP/servlet, and EJB.
The API outputs debugging information and can also be used to record information related to security conflicts during the program running. All this effectively ensures the robustness and maintainability of the program.
How can a log system minimize its impact on program performance? Java Logging
In terms of API design, we fully consider how to provide the lightest implementation, the smallest amount of code, and the most compact structure as much as possible. On the other hand, it also provides many interfaces and abstract classes, allowing programmers to easily expand their own functions. At the same time, Java Loggin
API provides a mechanism to dynamically change the output log information at runtime.
Although Java Logging
APIS provide powerful functions, but they are very easy to use. For some simple applications, Java Logging
API is a simple and efficient development kit. It also supports multi-level subscription and publishing, as well as filtered and prioritized log records.
Java Logging
The 15 classes and an interface contained in the API are put in the Java. util. Logging package at the same level. The following describes the key classes and interfaces:
Logger: log object, which is the main entity for applications to implement the logging function. Logger is used to record information about a specific system or application component. A logger object can have any string name. However, we recommend that you use a hierarchical package name and class name as its name to quickly locate the location where an error occurs. Of course, you can also create an "anonymous" logger object. Logger includes the following methods for logging:
●
A series of log () methods with different parameters. The parameters can be levels, messages, and optional parameters.
●
A series of LOGP () methods with different parameters. The letter P represents precision (precise). This series of methods are similar to the log method, but they can more clearly indicate the log recording classes and methods.
●
A series of logrb () methods with different parameters. The letter RB represents the bound Resource
Bundle), which can be used to output local logs. The name of the bound resource can also be provided directly in the create logger constructor.
●
Three methods that can trace the execution of other common methods, including entering (), existing (), and throwing. They can record the process of calling other methods, enter, exit, or throw an error.
In addition, the log () method can be used as a simple method to record logs, such as Serere (), warning (), and log. Corresponding log () methods.
Level: A level object that defines the level 1 standard record level and can be used to control the output of records. For example, you can configure a program to output only some records, while ignoring other levels of output. The level class defines the following level constants (Level constants): severe, warning, info, config, fine, finer, and finest. In addition, constant off (indicating logging off) and all (indicating that all information is required to be recorded) are defined ).
Handler: A processor object that outputs a logrecord object to various targets, including memory, output stream, console, file, or socket. Handler has multiple sub-classes for different output targets. You can simply call setlevel (level. Off) to disable a processor or re-open it to give a proper level. Developers can also compile Other Processors as needed, such as jmshandler or jdbchandler. Handler is an abstract class. JDK has several built-in implementation classes.
Logrecord: the log record object. It can be used to transmit detailed request records between the logging framework and a single logging handler, including the log Object Name, log message, level, and parameters, it even includes the class and method names that record logs. If the logger object does not explicitly specify the log source using the LOGP () method, logrecord will find its own log source based on the call.
Logmanager: logmanager is a global object in singleton mode. It manages multiple logger objects and Log service. By default, logmanager is the LIB/
Logging. properties reads the configuration information of the log system. Logmanager is based on two system attributes: Java. util. logging. config. class and Java. util. loggin. config. fiel is used to obtain the location of configuration information. Therefore, you can set the values of these two system attributes to change logger behavior.
Filter: Filter object, which provides a more precise control mechanism than the log level to filter log records and outputs. Logging
The API supports a general filter mechanism that allows applications to add any number of filter chains to control the output of records. The filter interface provided by the logging API has only one public Boolean method.
Isloggable (logrecord record) is used to determine whether the input logrecord object needs to be recorded.
Formatter: Format object. It supports formatting output of logrecord objects. In Java Logging
The API provides two formatting classes: simpleformatter and xmlformatter, which are used to format log records in plain text and XML formats respectively. Like processors, developers can also develop other formatters based on their own needs. Formatter is an abstract class. Its subclass must implement public string format (logrecord
Record) method.
Conclusion: After learning about Java logging
After these key classes are included in the API, let's take a look at the relationship between them. A logmanager manages multiple logger objects, and each logger object can contain a chain composed of multiple hanlders to complete hierarchical processing of log records. The processor handler and logger can have their own filter objects. At the same time, handler can also have a formatter object. Between the logging framework and the preceding objects, log information is encapsulated into a logrecord object for transmission.
PS: log4j, a well-known and commonly used logging tool, is provided by Apache ). Often witnessed, but never answered
Touch and use. Today, I roughly flipped through log4j. Most ideas and concepts and Java
The loggign API is similar.
I figured out the Java loggign API. I believe it is not difficult to read log4j again and then master log4j.