The JDK1.4 version of Sun is introduced in the Java.util. Logging package newly adds the core API function to process the application log work. This Java log package provides a simple and flexible way to embed multi-level logs for Java applications.
Log Logging package
In order to use the log package, you only need to get a logger object and call some of the plurality of records in the logger, such as info (String Message). The logger logger creates a logRecord (including the application specific log information) object and pass it to one or more handler, the Handler object outputs it to the target, so that the output of the log record is implemented. During the output of the log, the Logger and Handler objects can determine those logs need to be recorded according to the log level and filter (Filter). At the same time, Handler works with Formatter, formatter formats log information, and determines how log records output to the log. The timing chart of the application log log is shown below.
Let's take a very simple example of a very simple application logging log, as shown below:
Logger log = logger.getlogger (this.getclass (). Getname ());
Log.Info ("log message");
Log level
The Java log package is assigned to each log message to control the output of the log record. The level is a integer data, the higher the level of records, the greater this integer data. The following level is defined in the Level class (arranged in order from low to high):
Log hierarchical table
level
importance
Log Method
Level value
Severe
Very important
Severe (String Message);
1000
Warning
For warning
Warning (String Message);
900
Info
Information runtime message
Info (String Message);
800
Config
Static setting message
Config (String Message);
700
FINE
Provide tracking information
FINE (String Message);
500
Finer
Display detailed tracking information
Finer (String Message);
400
Finest
More detailed tracking information
FINEST (STRING MESSAGE);
300
All
Show all information that should log records
Unable
Integer.min_Value
Off
Close log
Unable
Integer.max_Value
You can specify a minimum log level for logger and handler, then only its level is equal to or higher than the log message of this minimum record level will be recorded. In addition to the log level, the Level class also defines the OFF level and all levels, OFF levels to close logging feature, all logs are not recorded. The ALL level log is recorded regardless of the minimum record level of Logger and Handler.
Handler object
The Handler object gets log information from the Logger and outputs it. You may output to the console, or output to the file, or send these log information to the log service in the network, or forward them to the operating system log, or what else. The JDK1.4 log API provides two Class of Handler objects. MemoryHandler simply saves log messages in a Circular Memory Buffer, which will then publish them to the target handler when a specific trigger event occurs. A typical trigger event is a log message that receives the level that matches the publishing level. If other publishing standards are required, the MemoryHandler class can extends, rewrite the log method to publish memory buffers in accordance with user-defined logging criteria. StreamHandler issues logging to the specified output stream. The Java Log API specifies three streamhandler objects. ConsoleHandler issues logging to the standard error stream. FileHandler Publish logging to the specified file, which can also be configured to write to the loop file set into the log record. SocketHandler issues log records (with other application communication).
Write your own JDBCHANDLER object
When we record the application log, we often encounter such a need, and the user wants to store the log into the database table. The usual approach is to write a log record class, which is responsible for writing the application's log to the database through JDBC. Now we can extend the Handler in the JDK1.4 log Logging package to meet the specific needs of the application.
We know that the Handler object is responsible for getting log information from Logger and outputs it to the destination. The user wants to store the log into the database, in fact, this is only replaced with the output target of the log information as a database. So write the JDBCHANDLER class that extends the Handler, which is written to the database to the database. To extend the Handler class, you must implement the three abstraction functions of the Handler definition (Abstract):
Public Abstract void flush ();
Public Abstract Void Publish (LogRecord Record);
Public abstract void close () throws java.lang.securityException;
Among them, the Publish (LogRecord Record method is most important, and it finally implements output log information to the target. The close () method is responsible for releasing all the resources owned by Handler. This time we can implement log information (packaged in the LogRecord object in the Publish (LogRecord Record) method, you can insert into the database through the appropriate JDBC driver. Below I will use the Oracle Database Output Logging as an example, the Publish method is implemented as follows:
Public void publish (Logrecord Record) {
Connection conn = NULL;
PreparedStatement PSTMT = NULL;
Try {
// Get Database Connection
CONN = getConnection ();
IF (conn! = null) {
Object [] Content = Record.getParameters ();
String SQL = "INSERT INTO MyLog (LogTime, EventType" VALUES (?,?)
PSTMT = conn.preparestatement (SQL);
PSTMT.SETLONG (1, Record.getMillis ());
Pstmt.setstring (2, (string) Content [0]); // Save Loginfo in Database
Pstmt.execute ();
}
} catch (exception e) {
E.PrintStackTrace ();
}
Finally {
// Release Connection
Destroy (CONN, PSTMT, NULL);
}
}
Now let's write to the database with JDBchandler just written, please see the following code segment:
Logger log = logger.getlogger ("");
JDBCHANDLER JDBC = New JDBCHANDLER ();
Log.AddHandler (JDBC);
String [] content = {"INSERT"};
LogRecord LR = New LogRecord (Level.info, "Second Test Message";
Lr.SetParameters (Content);
// first Insert Log
Log.log (Level.info, "First Mest Message", Content;
// Second Insert Log
Log.log (lr);
As can be seen from the above code, the program written to the log is not much change. The only thing that needs to be pointed out is that the logging logger is to output the log using the JDBCHandler object you have previously prepared. Specify the JDBCHandler object with the AddHandler method of the Logger object.
Conclude
You can also format the log information using the Format object, for example, can be used to generate the SQL statement required by JDBCHandler, which is formatted in formatting the log information data associated with the application. It is also possible to extend the LogRecord class to meet the specific needs in the real world, increase the specific content of the log output.
reference
Sun
company's
JDK1.4
version
Java.util. Log Logging Package
Source program.