log4j Source Reading

Source: Internet
Author: User

Source code reading based on log4j1.2.17

The Org.apache.log4j.xml.DOMConfigurator class is an XML configuration file initialization class for log4j

The Org.apache.log4j.PropertyConfigurator class is the initialization class for the Properties form configuration file of log4j

Today we're looking at the former.

  /**     A static version of {@link #doConfigure (String, Loggerrepository)}.  *  /static public  void Configure (String filename) throws Factoryconfigurationerror {    new Domconfigurator (). doconfigure (filename,       logmanager.getloggerrepository ());  }

Generally speaking, we're all talking. Log4j.xml configuration file is placed in the same directory as SRC, convenient to find in the current path of class path (cannot be placed in the SRC directory, or will be packaged into the class directory after packaging)

Public  void Doconfigure (final String filename, loggerrepository repository) {    parseaction action = new Parseaction () {public          Document parse (final Documentbuilder parser) throws Saxexception, IOException {              return Parser.parse (new File);          }          Public String toString () {               return "file [" + filename + "]";           }    };    Doconfigure (action, repository);  }  

Repositroy's role has not yet been seen

Let's look at how to parse XML and initialize related objects.

The method in the Domconfigurator class will eventually be called

/** used internally to configure the LOG4J framework by parsing a DOM tree of XML elements based on <a href       = "Doc-files/log4j.dtd" &GT;LOG4J.DTD&LT;/A&GT;.    */protected void Parse (element element) {String rootelementname = Element.gettagname (); if (!rootelementname.equals (Configuration_tag)) {if (Rootelementname.equals (Old_configuration_tag)) {LogLog.warn (" The < "+old_configuration_tag+" > element has been deprecated. "); Loglog.warn ("Use the <" +configuration_tag+ "> Element instead."); else {loglog.error ("DOM element is-not a <" +configuration_tag+ "> Element.");      Return          }} String Debugattrib = subst (Element.getattribute (internal_debug_attr));    Loglog.debug ("Debug attribute= \" "+ Debugattrib +" \ ".");    If the LOG4J.DTD is not specified in the XML file and then the//"Debug" attribute is returned as the empty string. if (!debugattrib.equals ("") &&!debugattrib.equals ("null")) {LogloG.setinternaldebugging (Optionconverter.toboolean (Debugattrib, true));      } else {loglog.debug ("ignoring" + internal_debug_attr + "attribute.");}      Reset repository before configuration if reset= "true"//on configuration element.    String Resetattrib = subst (Element.getattribute (reset_attr));    Loglog.debug ("Reset attribute= \" "+ Resetattrib +" \ "."); if (! (""). Equals (Resetattrib)) {if (Optionconverter.toboolean (Resetattrib, False)) {Repository.resetconfigura         tion ();    }} String Confdebug = subst (Element.getattribute (config_debug_attr)); if (!confdebug.equals ("") &&!confdebug.equals ("null")) {Loglog.warn ("the \" "+config_debug_attr+" \ "att      Ribute is deprecated. ");      Loglog.warn ("Use the \" "+internal_debug_attr+" \ "attribute instead.");    Loglog.setinternaldebugging (Optionconverter.toboolean (Confdebug, true)); } String thresholdstr = subst (Element.getattribute (threshold_ATTR));    Loglog.debug ("Threshold =\" "+ thresholdstr +" \ "."); if (! "". Equals (THRESHOLDSTR) &&! "    Null ". Equals (Thresholdstr)) {repository.setthreshold (THRESHOLDSTR);    }//hashtable appenderbag = new Hashtable (11); /* Building Appender objects, placing them in a local namespace for future reference *//first Configure each C    Ategory factory under the root element.    Category factories need to is configured before any of//categories they support.    String tagName = null;    Element currentelement = null;    Node currentnode = null;    NodeList children = Element.getchildnodes ();    Final int length = Children.getlength ();      for (int loop = 0; loop < length; loop++) {CurrentNode = Children.item (loop); if (currentnode.getnodetype () = = Node.element_node) {currentelement = (ELEMENT) Currentnode;tagname =  Currentelement.gettagname (); if (Tagname.equals (category_factory_tag) | | tagname.equals (LOGGER_FACTORY_TAG)) { ParsecaTegoryfactory (currentelement);}      }} for (int loop = 0; loop < length; loop++) {CurrentNode = Children.item (loop); if (currentnode.getnodetype () = = Node.element_node) {currentelement = (ELEMENT) Currentnode;tagname = Currentelement.gettagname (); if (Tagname.equals (CATEGORY) | | tagname.equals (LOGGER)) {parsecategory (currentelement) ;} else if (tagname.equals (Root_tag)) {parseroot (currentelement);} else if (Tagname.equals (Renderer_tag)) {Parserenderer    (currentelement);            } else if (Tagname.equals (Throwable_renderer_tag)) {if (repository instanceof Throwablerenderersupport) {            Throwablerenderer tr = parsethrowablerenderer (currentelement);            if (tr! = null) {((Throwablerenderersupport) repository). Setthrowablerenderer (TR); }}}} else if (! ( Tagname.equals (Appender_tag) | | Tagname.equals (Category_factory_tag) | | Tagname.equals (Logger_factory_tag)) {QuietparseunrecognizEdelement (repository, currentelement, props); }      }    }  }

By analyzing this method, we can see

1) log4j.xml

log4j Source Reading

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.