Analysis of Java.util.logging.Logger log generation process

Source: Internet
Author: User
Tags log log

The main classes in the Java.util.logging package are the following:

Logmanager    There is a single global Logmanager object that can be used to maintain a shared set of Logger and log services. Logger        Logger objects are used to record log messages for a particular system or application component LogRecord     LogRecord objects are used to pass log requests between the log framework and a single log Handler Handler       object to get log information from Logger and export the information Formatter     Formatter to provide support for formatting logrecords

In simple terms:

Logmanager is a factory that stores logger, each time we generate logger, if it is a new logger (logger name does not exist in Loggermanager), a new logmanager will be generated in logger, If it is an existing logger, then take it directly and use it.

Logger Logdemo = Logger.getlogger ("Log.demo");    // generate a log manager named Log.demo in the Logger Factory (Logmanager) .

When using logger to generate a log, it will first save the log in LogRecord, and LogRecord to handler for export work, of course, if you need to format the export content, you need to export the log before handler, Specifies a log format processor for handler, which is the work of formatter.

Logdemo.severe ("This is a severe log");    // record a log message with a log level of Level.severe

Run the program to see the console input message:

May 24, 2015 7:44:10 am Cn.kolbe.java.log.LogDemo main critical: This is a severe log

In this there is a question, said LogRecord is responsible for recording the message, handler responsible for the log export, formatter responsible for the log format, but why above we did not show the message to LogRecord, also did not show logger add handler, Even without adding formatter for handler, it automatically outputs messages in the console (SYSTEM.ERR)! To dismiss the question, let's take a look at the severe () method in the source code of Logger

1  Public classLogger {2      Public voidSevere (String msg) {3 log (Level.severe, msg);4     }5      Public voidlog (level, String msg) {6         if(!isloggable (level)) {7             return;8         }9         LogRecordLR =NewLogRecord (level, msg);Ten Dolog (LR); One     } A     Private voidDolog (LogRecord lr) { - lr.setloggername (name); -         FinalLoggerbundle lb =Geteffectiveloggerbundle (); the         FinalResourceBundle bundle =Lb.userbundle; -         FinalString Ebname =Lb.resourcebundlename; -         if(Ebname! =NULL&& Bundle! =NULL) { - Lr.setresourcebundlename (ebname); + Lr.setresourcebundle (bundle); -         } + log (LR); A     } at      Public voidlog (LogRecord record) { -         if(!isloggable (Record.getlevel ())) { -             return; -         } -Filter Thefilter =filter; -         if(Thefilter! =NULL&&!thefilter.isloggable (record)) { in             return; -         } to  +         //Post The LogRecord to all we handlers, and then to -         //Our parents ' handlers, all the the-the-the-the-the-tree. the  *Logger Logger = This; $          while(Logger! =NULL) {Panax Notoginseng             Finalhandler[] Loggerhandlers =Issystemlogger -?logger.accesscheckedhandlers () the : logger.gethandlers ();  +  A              for(Handler handler:loggerhandlers) { the Handler.publish (record); +             } -  $             Final BooleanUseparenthdls =Issystemlogger $?logger.useparenthandlers - : Logger.getuseparenthandlers (); -  the             if(!Useparenthdls) { -                  Break;Wuyi             } the  -             logger = Issystemlogger? Logger.parent:logger.getParent (); Wu         } -     } About}

From the source can be seen

1) The severe () method calls the public void log (level, String msg) method, setting the log message levels to Level.severe,

2) in the log (level, String msg) method, you can see that it generates a LogRecord object, which is the log message object

3) then calls the private void Dolog (LogRecord lr) method, which mainly sets the localization language pack, skipping

4) Finally call the public void log (LogRecord record) method, where there is a while loop that first obtains the handler object already set in the current logger object through the Gethandlers () method in the Logger class , and then call the Publish () method in the handler class to print the log message record (but we didn't set the handler?). Then look down)

5) Logger = Issystemlogger can be seen underneath the loop body? Logger.parent:logger.getParent (); The purpose of this statement is to traverse the parent object in the Logger object and pass LogRecord to the parent object's handler

6)

Logger parent = logdemo.getparent (); System.out.println ("Parent.getname ():" + parent.getname ()); System.out.println ("Parent.getclass (). GetName ():" + Parent.getclass (). GetName ());

Console input:

Parent.getname (): Parent.getclass (). GetName (): Java.util.logging.logmanager$rootlogger

The default root log object for discovery logger is an object of Logmanager's inner class Rootlogger class, and the root log object's name is a white space character!

Let's verify that:

Logger Rootlog = Logger.getlogger (""); System.out.println ("Rootlog.getclass (). GetName ():" + Rootlog.getclass (). GetName ());

Console output:

Rootlog.getclass (). GetName (): Java.util.logging.logmanager$rootlogger

Let's also look at the root log for a default handler object:

handler[] handlers = rootlog.gethandlers ();  for (int i = 0; i < handlers.length; i++) {System.out.println (Handlers[i].getclass (). GetName ());}

Console output:

Java.util.logging.ConsoleHandler

The original root log is installed by default Consolehandler (corresponding to the console output) so when we generate the log message does not display the specified handler, it can also enter the log message in the console, you may ask, when the root log is generated? It was assembled by the Logmanager object the first time we called Logger.getlogger ("log name") in our application. The process will be resolved the next time.

Note: The level is ignored (log log level): Used to control log publishing levels, as well as filter (log log filter): Used to authenticate whether the message should be published to LogRecord or discarded directly. This piece of content will be found in a time to fill up.

Analysis of Java.util.logging.Logger log generation process

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.