Java.util.logging.Logger use of the detailed

Source: Internet
Author: User
Tags system log log4j

Java.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 object

Static 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 logger is more detailed than the level of log4j, 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 = INFOThird, 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"); } }
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:020

As can be seen here, Logger's name has a hierarchical relationship. This is in full agreement with the Log4j control method.

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. Four Logger's handlerThe 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:

 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);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 Result:

true 2009-7-28 20:36:142009-7-28 20:36:142009-7-28 20:36:142009-7-28 20:36:14  0

View drive C:

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. Logger's Formatterformatter 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 this example to understand:

 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);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:

Files in control and C-drive output

In this context, Java's own logger content is gone.

The feeling format is very uncomfortable. The custom format sucks.

  

Java.util.logging.Logger use of the detailed

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.