Introduction of automatic loading of log4j

Source: Internet
Author: User
Tags log4j

Original address: http://www.cnblogs.com/alipayhutu/archive/2013/04/18/3028249.html

Look at the code today, find Log4j.properties. There is no corresponding load code, but it is in effect, it's amazing.

Look inside, org.apache.log4j, Logmanager.java, which has a static method block:

  static {//By default we use a defaultrepositoryselector which always returns ' H '.
    Hierarchy h = new hierarchy (new Rootlogger (level) level.debug));

    Repositoryselector = new Defaultrepositoryselector (h);  /** Search for the properties file Log4j.properties in the CLASSPATH. 

    */String override =optionconverter.getsystemproperty (Default_init_override_key, NULL); If there is no default init override, then get the resource//specified by the user or the default Config fi
    Le. if (override = NULL | |
                              ' False '. Equalsignorecase (override)) {String configurationoptionstr = Optionconverter.getsystemproperty (

      Default_configuration_key, NULL); String configuratorclassname = Optionconverter.getsystemproperty (confi

      Gurator_class_key, NULL);

      URL url = null; If theUser has not specified the Log4j.configuration//property, we search the file "Log4j.xml" and then ' Log4j.properties ' if (configurationoptionstr = = null) {URL = Loader.getresource (default_xml_configuratio
    N_file);
    if (url = = null) {URL = Loader.getresource (default_configuration_file);
    } else {try {url = new URL (configurationoptionstr); \ catch (Malformedurlexception ex) {//So, resource are not a URL://Attempt to get the resource from the CLA 
    SS Path URL = loader.getresource (CONFIGURATIONOPTIONSTR); }//If We have a non-null URL, then delegate the rest of the the//configuration to the Optio
      Nconverter.selectandconfigure//method.      
    if (URL!= null) {loglog.debug ("Using URL [" +url+ "] for automatic log4j configuration."); Optionconverter.selectandconfigure (URL, configuratorclassname, Logmanager.getloggerrepositoRy ());
      else {Loglog.debug ("could not find resource: [" +configurationoptionstr+ "].");  }
    }  
  }

In other words, what is it. Other words:

1. Get the system properties to see if the user set the override. Default is not set.

2. If there is no setting, then try to find, there is no log4j.xml, there is load.

3. If not, then try to find out, there is no log4j.properites, there is loading.

Among them, 2, 3 mentioned in the "try to find", may be to which directory to find it. Translated, the effect is not good, or the original text clear: Search for resource using the thread context class loader under JAVA2. If that is fails, the search for resource using the class loader to loaded this class (loader). Under JDK 1.1, only the "class loader" Loaded this class (loader) is used. Try one last time with Classloader.getsystemresource (Resource), which is being using the System class loader in JDK 1.2 and VI Rtual Machine ' s built-in class loader in JDK 1.1.

So, if you put Log4j.xml or log4j.properties in these directories, then log4j will "automatically load" to, without the application of manual write load code.

But personally, I still tend to write loads myself. Because this "quietly be done by people," one is difficult to understand the code, the second is if a classmate put a log4j,b classmate and wrote a place in other directories, this default loading mechanism, not necessarily which entry into force and the Order of entry into force. This uncertainty, or write their own two lines of code, to eliminate in the cradle.

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.