Java.util.logging.Logger use of the detailed

Source: Internet
Author: User
Tags 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 example

 Public classTestlogger { Public Static voidMain (string[] args) {Logger log= Logger.getlogger ("Lavasoft");                 Log.setlevel (Level.info); Logger Log1= Logger.getlogger ("Lavasoft"); SYSTEM.OUT.PRINTLN (log==LOG1);//trueLogger log2 = Logger.getlogger ("Lavasoft.blog");                 Log2.setlevel (level.warning); Log.info ("AAA"); Log2.info ("BBB"); Log2.fine ("Fine"); } }
Output Result:
True 2009-7-28 20:00:300
when commenting out Log2.setlevel (level.warning);
Output Result: true 2009-7-28 20:02:022009-7-28 20:02:02
As can be seen here, 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

 Public classTestlogger { Public Static voidMain (string[] args)throwsIOException {Logger log= Logger.getlogger ("Lavasoft");                 Log.setlevel (Level.info); Logger Log1= Logger.getlogger ("Lavasoft"); SYSTEM.OUT.PRINTLN (log==LOG1);//trueLogger log2 = Logger.getlogger ("Lavasoft.blog"); //Log2.setlevel (level.warning);Console ControllerConsolehandler Consolehandler=NewConsolehandler ();                 Consolehandler.setlevel (Level.all); Log.addhandler (Consolehandler);
File controller Filehandler Filehandler=NewFilehandler ("F:/testdir/8888g.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:142009-7-28 20:36:142009-7-28 20:36:142009-7-28 20:36:14

View F:/testdir/8888g.log:

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
 Public Static voidMain (string[] args) {Try{Filehandler Filehandler=NewFilehandler ("F:/testdir/8888g.log");            Filehandler.setlevel (Level.info); Filehandler.setformatter (NewFormatter () {//defines an anonymous class @Override PublicString format (LogRecord record) {returnRecord.getlevel () + ":" + record.getmessage () + "\ n";            }            });            Logger.addhandler (Filehandler); Logger.info (Test); } Catch(SecurityException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }

Java.util.logging.Logger use of the detailed

Related Article

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.