The Handler object initializes the default value of the relevant attribute of the LogManager object when creating (such as the "Object Properties of the Handler's Filter, Formatter, Level, etc.).
The Handler object can be paused by calling setLevel (Level.off); set the appropriate record log message level recovery by calling SetLesel.
Handler is an abstract class. In J2SDK1.4, the relationship between its subclasses and them see Figure 1.
1. Subclasses of MemoryHandler Handler, a loop buffer in memory for cache logging requests. Usually MemoryHandler only simply stores incoming logRecords into its memory. The overhead of this cache is very low, it removes the system consumption generated by formatting. When a trigger condition is satisfied, MemoryHandler puts its buffer data Push to the target handler, which performs the actual output by the latter. There are three modes that trigger MemoryHandler for PUSH operations: A. The level of incoming logRecords is higher than that of MemoryHandler pre-defined PUSH level; B, there are other objects explicitly call their PUSH method; C, its subclass is overloaded LOG method, Retrieve each incoming logRecords one by one, if the PUSH operation is performed in line with a specific standard.
Example: Suppose we need to track a rare bug that appears in a production environment. In most cases, systematization produces a large number of log records, and we only care about the recent records in the record, then we only need to use MemoryHandler to cache logging, and only a few recent events only when an event occurs Record from dump to the formulated file from the memory.
//MemoryHandlertest.java
Import java.util.logging. *;
Import java.io. *;
Public class memoryhandlertest {
FileHandler Fhandler;
Logger logger;
MemoryHandler Mhandler;
MemoryHandlertest () {
Try {
/ / Configure a logging file called my.log
Fhandler = New FileHandler ("my.log");
INT NUMREC = 5;
// Construct a 5 log recorded MemoryHandler,
//The target handler is a FileHandler
Mhandler = New MemoryHandler (Fhandler, NumRec, Level.off);
// Construct a recorder
Logger = logger.getlogger ("com.mycompany");
/ / Add a MemoryHandler for the logger
Logger.addhandler (Mhandler);
} catch (ioexception e) {
}
}
Public static void main (string args []) {
MemoryHandlertest MT = New MemoryHandlertest ();
INT TRIGGER = (int) (Math.random () * 100);
For (INT I = 1; I <100; i ) {
// Currently a logging in MemoryHandler
Mt.Logger.log (Level.info, "Logging" i);
IF (i == Trigger) {
// Trigger the event to be established, explicitly call MemoryHandler
/ / Push method triggers the target handler output log record
//my.log file
Mt.mHandler.push ();
Break;
}
}
}
}
Example 2
2, FileHandler file processor.
The StreamHandler flow processor outputs the log in the stream. FileHandler, consolehandler, SocketHandler subclass for StreamHandler. ConsoleHandler outputs the log record to the control terminal. The previous example (except for example 2) outputs the diary record data to the console.
FileHandler outputs logging to a specific file, or in several log files. The log file can set the capacity size. When the log file reaches the limited capacity, it will be automatically emptied, and the weight begins to write new log record data.
Example: Create a file processor with a capacity of 1MB
INT LIMIT = 1000000; // 1 MB
FileHandler FH = New FileHandler ("My.LOG", LIMIT, 1);
For loop log files, each file will be specified. When the length of the current log file reaches the set value, the file is turned off, the new log file is created, the old file will add the serial number after the file name template. The log record file is generated in multiple order numbers.
example:
Try {
// Create a file processor with 3 log files, each capacity is 1MB
String Pattern = "MY% g.log";
INT LIMIT = 1000000; // 1 MB
INT Numlogfiles = 3;
FileHandler FH = New FileHandler (Pattern, Limit, Numlogfiles);
...
} catch (ioexception e) {
}
(4) Level
The Level object defines the level of a set of log messages to control the output of log messages. One level corresponds to a integer value. The log message level schemes priority according to the size of its integer value. Set a level in the Logger object, the log message greater than or equal to this level will be passed to a Handler object for output processing.
The following message level is defined in the J5SDK1.4 Java Logging framework:
Level name
INT value (Windows 2000 environment)
Off
2147483647
Severe
1000
Warning
900
Info
800
Config
700
FINE
500
Finer
400
Finest
300
All
-2147483648
Table I
Level.off has the highest level. Set Logger's Level Level to level.off
Let us see how the message level is working: LoggingLeveltest.java
Import java.util.logging. *;
Public class loggingLeltest {
Public static void main (string args []) {
// Get an anonymous logger using the statically manner of Logger
Logger logger1 = logger.getanonymouslogger ();
/ / Set the minimum log message level recorded by the logger object
Logger1.Setlevel (Level.Finer);
// Record the message
Logger1.severe ("Severe Level Message");
Logger1.warning ("Warning Level Message");
Logger1.config ("ConfiG Level Message");
Logger1.info ("Info Level Message");
Logger1.fine ("FINE Level Message");
Logger1.finer ("FineR Level Message");
Logger1.finest ("Finest Level Message");
}
}
Example 3
operation result:
2003-1-15 7:02:03 Loggingleveltest Main
Server: SEVERE level message
2003-1-15 7:02:04 loggingLeveltest Main
Warning: warning level message
2003-1-15 7:02:04 loggingLeveltest Main
Configuration: config level message