Java. util. logging. Logger Usage Details, java. util. logging
Java. util. logging. logger is nothing new, and it will be available in 1.4. But because of the existence of log4j, this logger has been silenced. In fact, in some testing code, the built-in logger of jdk is more convenient than log4j.1. Create a Logger objectStatic Logger getLogger (String name)
Searches for or creates a logger for a specified subsystem.
Static Logger getLogger (String name, String resourceBundleName)
Searches for or creates a logger for a specified subsystem. Note: name is the name of Logger. When the name is the same, only one Logger with the same name is created.2. Logger levelMore detailed than the log4j level, all defined inJava. util. logging. Level. The levels are listed in descending order as follows:
- SEVERE (maximum value)
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST (minimum value)
In addition, there is a level OFF that can be used to Disable Logging and enable logging for ALL messages with level ALL. The default logger level is INFO. Logs lower than INFO are not displayed. The default Logger level is defined under the lib of the jre installation directory. # Limit the message that are printed on the console to INFO and above.
Java. util. logging. lelehandler. level = INFO
Iii. Simple instancesPublic class TestLogger {
Public static void main (String [] args ){
Logger log = Logger. getLogger ("lavasoft ");
Log. setLevel (Level. INFO );
Logger log1 = Logger. getLogger ("lavasoft ");
System. out. println (log = log1); // true
Logger log2 = Logger. getLogger ("lavasoft. blog ");
Log2.setLevel (Level. WARNING );
Log.info ("aaa ");
Log2.info ("bbb ");
Log2.fine ("fine ");
}
} True
2009-7-28 20:00:30 TestLogger main
Information: aaa
Process finished with exit code 0 when you comment out log2.setLevel (Level. WARNING );
Output result: true
2009-7-28 20:02:02 TestLogger main
Information: aaa
2009-7-28 20:02:02 TestLogger main
Information: bbb
Process finished with exit code 0 from here we can see that the logger name is hierarchical. This is exactly the same as the control method of log4j. Below is the original document of the API: Generally, the hierarchical namespace separated by dots is used to name Logger. Logger names can be any string, but they should generally be based on the package name or Class Name of the recorded component, such as java.net or javax. swing. In addition, you can create an anonymous Logger whose name is not stored in the Logger namespace. You can call a getLogger factory method to obtain the Logger object. These methods either create a new Logger or return a suitable existing Logger.
4. Logger HandlerThe Handler object obtains the log information from the Logger and exports the information. For example, it can write the information to the console or file, send the information to the Network Log service, or forward it to the operating system log.
You can disable Handler by executing setLevel (Level. OFF) and re-enable it by executing the appropriate setLevel.
The Handler class usually uses the LogManager attribute to set the default values of the Filter, Formatter, and Level of the Handler. Java. util. logging. Handler
Java. util. logging. MemoryHandler
Java. util. logging. StreamHandler
Java. util. logging. lelehandler
Java. util. logging. FileHandler
Java. util. logging. SocketHandler example: public class TestLogger {
Public static void main (String [] args) throws IOException {
Logger log = Logger. getLogger ("lavasoft ");
Log. setLevel (Level. INFO );
Logger log1 = Logger. getLogger ("lavasoft ");
System. out. println (log = log1); // true
Logger log2 = Logger. getLogger ("lavasoft. blog ");
// Log2.setLevel (Level. WARNING );
Lelehandler ConsoleHandler = new consoleHandler ();
Lelehandler. setLevel (Level. ALL );
Log. addHandler (lelehandler );
FileHandler fileHandler = new FileHandler ("C:/testlog % g. log ");
FileHandler. setLevel (Level. INFO );
Log. addHandler (fileHandler );
Log.info ("aaa ");
Log2.info ("bbb ");
Log2.fine ("fine ");
}
} Output result: true
2009-7-28 20:36:14 TestLogger main
Information: aaa
2009-7-28 20:36:14 TestLogger main
Information: aaa
2009-7-28 20:36:14 TestLogger main
Information: bbb
2009-7-28 20:36:14 TestLogger main
Information: bbb
Process finished with exit code 0 View Drive C: visible, the default log format is xml, very bad. Therefore, it is best to customize the logger format. It must be defined by Formatter.
5. Logger FormatterFormatter supports formatting LogRecords.
Generally, each logging Handler has an associated Formatter. Formatter accepts LogRecord and converts it to a string.
Some formatter (such as XMLFormatter) needs to wrap the header and tail strings around a set of formatting records. You can use the getHeader and getTail methods to obtain these strings. The LogRecord object is used to transmit log requests between the log framework and a single log Handler.
LogRecord (Level level, String msg)
Construct a LogRecord with a given level and message value. Java. util. logging. Formatter
Java. util. logging. SimpleFormatter
Java. util. logging. XMLFormatter: public class TestLogger {
Public static void main (String [] args) throws IOException {
Logger log = Logger. getLogger ("lavasoft ");
Log. setLevel (Level. INFO );
Logger log1 = Logger. getLogger ("lavasoft ");
System. out. println (log = log1); // true
Logger log2 = Logger. getLogger ("lavasoft. blog ");
// Log2.setLevel (Level. WARNING );
Lelehandler ConsoleHandler = new consoleHandler ();
Lelehandler. setLevel (Level. ALL );
Log. addHandler (lelehandler );
FileHandler fileHandler = new FileHandler ("C:/testlog % g. log ");
FileHandler. setLevel (Level. INFO );
FileHandler. setFormatter (new MyLogHander ());
Log. addHandler (fileHandler );
Log.info ("aaa ");
Log2.info ("bbb ");
Log2.fine ("fine ");
}
}
Class MyLogHander extends Formatter {
@ Override
Public String format (LogRecord record ){
Return record. getLevel () + ":" + record. getMessage () + "\ n ";
}
} Output: When the files output on the control and drive C are reached, the Logger content that comes with Java is gone.