Java.util.logging.Logger use explanation (turn)

Source: Internet
Author: User
Tags string format system log log4j

http://lavasoft.blog.51cto.com/62575/184492/

*************************************************

Java.util.logging.Logger Use of the detailedJava.util.logging.Logger is not something new, 1.4 has, but because of the existence of log4j, this logger has been silent, in fact, in some of the test code, the JDK comes with logger more convenient than log4j. First, create logger objectStatic Logger GetLogger (String name)
Finds or creates a logger for the specified subsystem.
Static Logger GetLogger (string name, String resourcebundlename)
Finds or creates a logger for the specified subsystem. Note: Name is logger, and when the name is the same, only one of the logger of the same name is created. second, the level of loggerMore detailed than the log4j level, all defined in Java.util.logging.LevelInside. The levels are sorted in descending order as follows:
    • SEVERE (highest value)
    • WARNING
    • INFO
    • CONFIG
    • FINE
    • Finer
    • FINEST (lowest value)
In addition, there is a level off that can be used to turn off logging and use level all to enable logging for all messages. Logger The default level is info, and logs that are lower than info are not displayed. The default level definition for logger is under Lib in the JRE installation directory. # Limit The message that is printed on the console to INFO and above.
Java.util.logging.ConsoleHandler.level = INFO Three, simple examplePublicStatic voidMain (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
Info: AAA

Process finished with exit code 0 when commenting out Log2.setlevel (level.warning);
Output Result:true
2009-7-28 20:02:02 Testlogger Main
Info: AAA
2009-7-28 20:02:02 Testlogger Main
Info: BBB

Process finished with exit code 0 it can be seen from here that logger's name has a hierarchical relationship. This is in full agreement with the Log4j control method. The following is the source of the API documentation:You typically use a dot-delimited hierarchy namespace to name Logger. Logger names can be arbitrary strings, but they should generally be based on the package name or class name of the component being logged, 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 obtain a Logger object by calling a GetLogger factory method. These methods either create a new Logger, or return an appropriate existing Logger. Iv. The handler of loggerThe Handler object obtains the log information from the Logger and exports the information. For example, it can write this information to a console or file, or it can send it to a blog service or forward it to the operating system log.
You can disable Handler by executing setLevel (Level.off) and re-enable it by performing the appropriate level of setLevel.
The Handler class typically uses the Logmanager property to set the default values for the Filter, Formatter, and level of the Handler. Java.util.logging.Handler
Java.util.logging.MemoryHandler
Java.util.logging.StreamHandler
Java.util.logging.ConsoleHandler
Java.util.logging.FileHandler
Java.util.logging.SocketHandler Example: publicStatic voidMain (string[] args)throwsIOException {
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);

Consolehandler Consolehandler =NewConsolehandler ();
Consolehandler.setlevel (Level.all);
Log.addhandler (Consolehandler);
Filehandler Filehandler =NewFilehandler ("C:/testlog%g.log");
Filehandler.setlevel (Level.info);
Log.addhandler (Filehandler);
Log.info ("AAA");
Log2.info ("BBB");
Log2.fine ("Fine");
}
} Output:true
2009-7-28 20:36:14 Testlogger Main
Info: AAA
2009-7-28 20:36:14 Testlogger Main
Info: AAA
2009-7-28 20:36:14 Testlogger Main
Info: BBB
2009-7-28 20:36:14 Testlogger Main
Info: BBB

Process finished with exit Code 0 View C drive: Visible, the default log mode is XML format, very bad. So it's best to customize the next logger format. Need to be defined with formatter. v. The formatter of LoggerFormatter provides support for formatted logrecords.
In general, each log record Handler has an associated Formatter. Formatter accepts the LogRecord and converts it to a string.
Some formatter (such as xmlformatter) need to wrap headers and trailing strings around a set of formatted records. You can use the GetHeader and GetTail methods to get these strings. The LogRecord object is used to pass log requests between the log framework and a single log Handler.
LogRecord (level, String msg)
Constructs a logrecord with a given level and message value. Java.util.logging.Formatter
Java.util.logging.SimpleFormatter
Java.util.logging.XMLFormatter See an example to understand: publicStatic voidMain (string[] args)throwsIOException {
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);

Consolehandler Consolehandler =NewConsolehandler ();
Consolehandler.setlevel (Level.all);
Log.addhandler (Consolehandler);
Filehandler Filehandler =NewFilehandler ("C:/testlog%g.log");
Filehandler.setlevel (Level.info);
Filehandler.setformatter (NewMyloghander ());
Log.addhandler (Filehandler);

Log.info ("AAA");
Log2.info ("BBB");
Log2.fine ("Fine");
}
}

classMyloghanderextendsFormatter {
@Override
PublicString format (LogRecord record) {
returnRecord.getlevel () +":"+ record.getmessage () +"\ n";
}
Output: In the control and C-drive output of the file, Java comes with the logger content is gone. The feeling format is very uncomfortable. The custom XML format sucks.

Java.util.logging.Logger use explanation (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.