LOG4J-Automatic loading principle and non-SRC use

Source: Internet
Author: User
Tags deprecated log4j

It is sometimes found that log4j.properties or log4j.xml are placed directly under SRC (i.e., compiled wen-inf/classes) in the project, and the project automatically loads and prints the log.

"1" Automatic loading principle

After the JVM loads the Log4j class (Org.apache.log4j.LogManager), executes the static code block and tries to load the log4j.properties or log4j.xml under the default path (wen-inf/classes):

public class Logmanager {/** * @deprecated this variable are for internal use only.
   It'll * become package protected in the future versions.

  * */static public final String default_configuration_file = "Log4j.properties";  

  Static final String default_xml_configuration_file = "Log4j.xml"; /** * @deprecated This variable are for internal use only.
   It'll * become private in the future versions.

  * */static final public String default_configuration_key= "Log4j.configuration"; /** * @deprecated This variable are for internal use only.
   It'll * become private in the future versions.

  * */static final public String configurator_class_key= "Log4j.configuratorclass"; /** * @deprecated This variable are for internal use only.
  It'll * become private in the future versions.
*/public static final String Default_init_override_key = "Log4j.defaultinitoverride"; ...//Part static code 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 file. if (override = = NULL | |
                              "false". Equalsignorecase (Override)) {String configurationoptionstr = Optionconverter.getsystemproperty (

      Default_configuration_key, NULL);

String configuratorclassname = optionconverter.getsystemproperty (Configurator_class_key, NULL);

      Note the following code--note the following code--note the following code--notice the URL of the code below = NULL; If the user has not specified the Log4j.configuration//property, we search first for the file "Log4j.xml" and T Hen//"log4j.properties" if (Configurationoptionstr == null) {URL =loader.getresource (default_xml_configuration_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 Resou 
            Rce from the class path URL = Loader.getresource (CONFIGURATIONOPTIONSTR); }   
      }
      ...//
 }

The two configuration files that are loaded are configured as shown in the following illustration:

"2" under non-src path

If Log4j.properties or log4j.xml is placed under a different path, such as Src/config, the configuration file can be loaded in Spring.xml or Web. XML, as follows:

Spring mode loaded, configured in Web. xml:

Spring loads Log4j.properties, which provides a log4jconfiglistener that itself can load log4j configuration files and log4j output paths from a specified location via the Web. XML configuration.

It is important to note that Log4jconfiglistener must be before spring's listener.

<!--set the LOG4J profile location loaded by sprng--

<context-param>  

    <param-name>log4jconfiglocation</ param-name>  

    <param-value>WEB-INF/classes/config/log4j.properties</param-value>  

</ Context-param>  

  <!--spring refreshes the interval for log4j configuration file changes in milliseconds--

<context-param>  

    <param-name >log4jRefreshInterval</param-name>  

    <param-value>10000</param-value>  

</ context-param>  

<listener>  
    <listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>  

</listener>

The configuration parameters are illustrated as follows:

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.