2003-1-15 7:02:04 loggingLeveltest Main
Information: INFO level message
It can be seen that the priority is lower than the log message of INFO is not recorded.
Level's constructor is easy to develop its own message level class for Protected.
Import java.util.logging. *;
// Custom Message Level
Class Mylevel Extends Level {
/ / Define your own message level SYSE
Public Static Final Level Sy = New Mylevel ("Syse", Level.severe.intValue () 10);
Public mylevel (String ln, int V) {
Super (ln, v);
}
}
Public class myleveltest {
Public static void main (string args []) {
Logger logger1 = logger.getanonymouslogger ();
// Set the message level
Logger1.Setlevel (Mylevel.syse);
// Record the message
Logger1.log (Mylevel.Syse, "SYSE message");
Logger1.severe ("Svere Message");
}
}
Example 4
operation result:
2003-1-15 15:40:04 Myleveltest Main
SYSE: SYSE message
Only the SYSE message is recorded, the Svere message is not recorded because the custom level SYSE is higher than Severe.
(5) Formatter
Formatter is responsible for formatting LogRecords. Each recording processor Handler is associated with the formatter object. The Formatter object receives LogRecord from Handler, and format it into a string and returns to Handler to output.
Formatter is an abstract class. In J2SDK1.4, its subclasses and the relationship between them are shown in Figure II.
Customize the formatter class. Example: MyFormattertest.java
Import java.util.date;
Import java.util.logging. *;
// Create each log log in the log format:
// Time
Class myformatter extends formatter {
Public String Format (LogRecord REC) {
StringBuffer BUF = New StringBuffer (1000);
BUF.Append (new date (). tolocalestring ()); // Time
BUF.Append ('');
BUF.Append (Rec.getlevel ()); // Message Level
BUF.Append ('');
BUF.Append (Rec.getMillis ()); // As a message ID
BUF.Append ('');
BUF.Append (FormatMessage (REC)); // Format log record data
BUF.Append ('/ n'); //
Return buf.tostring ();
}
}
Public class myformattertest {
Public static void main (string args []) {
// Create a recorder
Logger log1 = logger.getlogger ("MyLogger");
// Create a record processor
Handler MH = New consolehandler (); // Set Formatter for the record processor
MH.SetFormatter (new myformatter ());
/ / Add a record processor for the logger
Log1.addhandler (MH);
// Disable message processing to upload log messages to parent processor
Log1.setuseparentHandlers (false);
// Record the message
Log1.severe ("Message 1");
Log1.warning ("Message 2");
Log1.info ("message 3");
Log1.config ("Message 4");
}
}
Example 5
Program operation results:
2003-1-15 16:59:38 SEVERE 1042621178968 Message 1
2003-1-15 16:59:40 WARNING 10426211178985 Message 2
2003-1-15 16:59:41 Info 1042621179105 Message 3
Third, configuration file
JAVA Logging Frame of J5SDK1.4 (Windows):
% J2SDK1.4_HOME% / JRE / LIG / Logging.properties
From the configuration file you can see:
(1) Custom log configuration file:
Java -djava.util.logging.config.file = myfile
(2) Global Handler is loaded when Java VM is started.
(2) Global Handler defaults to java.util.logging.consolehandler.
Handlers = java.util.logging.consolehandler
So our logging action will be displayed on the console.
(3) The default message recording level is: info
.level = info
In the default, we can't see log messages below the INFO level in the console.
(4) The default HANDLER message format is java.util.logging.simpleformatter
Fourth, the application of the log frame in program test
The Logger class provides two ways: logger.entering () ogger.exiting (). This provides a convenient way to debug their method calls.
example:
The input parameters and output parameter methods called by the recording method MYMETHOD add an int after an object.
Running the program should be logging.properties
Java.util.logging.consolehandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.Consolehandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ch = info
Change to:
Java.util.logging.consolehandler.Logging.ConsoleHandler.Logging.consolehandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Logging.ConsoleHandler.Level = All
Import java.util.logging. *;
Public class myclass {
Public String Mymethod (int P1, Object P2) {
Logger logger = logger.getlogger ("com.mycompany.myclass);
IF (Logger.ISLoggable (Level.Finer) {
Logger.entering (this.getclass (). getname (), "mymethod",
New Object [] {new integer (p1), p2});
}
String TMP = p2.toString () P1;
IF (Logger.ISLoggable (Level.Finer) {
Logger.exiting (this.getClass (). getname (), "mymethod", tmp);
Return TMP;
}
Public static void main (string args []) {
Myclass mc = new myclass ();
String rslt = mc.mymethod (123, "hello");
}
}
postscript
J2SDK1.4
The introduced logging framework provides a convenient solution for building a simple logging system. Although there are still some dedicated log packs such as
Log4j
But from a simple print output to a strict, scalable logging framework,
J2SDK1.4
The log system is sufficient to meet the requirements of general system development.