[log4j] log4j configuration and configuration file load order

Source: Internet
Author: User
Tags log4j

Requirements: Place the log4j.properties configuration problem in the specified folder outside the project. Specific reasons refer to http://blog.csdn.net/lihe2008125/article/details/7764247.

By default, log4j automatically loads the configuration file in Classpath. And log4j.properties that are not packaged take precedence over log4j.properties files that are larger in the jar package. (Here you can recall the configuration in your project)

You can also configure a listener in the web.xml of a Web project.

There are two ways to think of it: 1 log4j supports using Java classes to configure what configuration files you use. 2 The use of class loading mechanism, the Log4j.properties file loaded into the classpath of the front, let log4j to load themselves.


Because you want to deploy multiple instances and do not want to modify the Java program, the 2nd method is used.


The project has been taken to add in Tomcat's catalina.sh

java_opts= "$JAVA _opts-dlog4j.configuration=file:/lc_iims/properties/crawl26101/log4j.properties-xx:maxpermsize =256m-xms256m-xmx1024m "
To resolve the log4j configuration file problem.


Problems encountered: The Log4j.properties file is loaded into the front of the classpath path, but does not play a role.

Reason: At this point log4j will first load the log4j.properties file in the jar package.


Principle, you can refer to the following source Org.apache.log4j.LogManager class.

Part of the source code is as follows:

Static/*/{* * * * * * Hierarchy H = new hierarchy (new Rootlogger (Level.debug));
Tor = new Defaultrepositoryselector (h);
*/*////* */String override = Optionconverter.getsystemproperty ("log4j.defaultinitoverride", null); /* *//* * (override!= null) && (!
False ". Equalsignorecase (override))) * */return;
/* A/a String Configurationoptionstr = Optionconverter.getsystemproperty ("log4j.configuration", null); /* *////String Configuratorclassname = Optionconverter.getsystemproperty ("Log4j.configuratorclass", NULL)
;
*/* * * * * URL url = null; */*/*/if (CONFIGURATIONOPTIONSTR = null) {/* * * * URL = loader.getresource ("Log4j.xml");/* 1
A/if (url = = NULL)/*/url = loader.getresource ("log4j.properties"); */* * * */ELSE {/* */try {/* * * * url = new URL (configurationoptionsTR); /*/*/* */catch (Malformedurlexception ex)/*/{/* * * * + URL = loader.getres
Ource (CONFIGURATIONOPTIONSTR); */*/*/*////////*//* * 122 * * * */if (URL!= null) {/* 123/* LOGLOG.DEBUG ("Usi
ng URL ["+ URL +"] for automatic log4j configuration. "); /*/try {/* * * * * optionconverter.selectandconfigure (URL, configuratorclassname, getloggerrepository
()); /*/*/* */catch (Noclassdeffounderror e) {/* 128/Loglog.warn ("Error during default INI
Tialization ", e); */* */*/} else {/* 131 * * * LOGLOG.DEBUG ("Could not find resource: [" + Configurationoptionst
R + "]."); /*     */     }
/*     */   }
/*     */ }


You can see support for log4j.defaultinitoverride and log4j.configuration and Log4j.configuratorclass system property configurations. You can see that the main call

url = Loader.getresource ("Log4j.xml");
/* */if (URL = null)
* */url = loader.getresource ("log4j.properties");


This is done in the Loader.getresource (String) method:

public static URL GetResource (String Resource)/*/{* */ClassLoader ClassLoader = null; * *
RL URL = null; * * * * * * * * * * * * * * * * * * */if (! JAVA1)) {* * * * * ClassLoader = GETTCL ();//if (ClassLoader!= null) {* * * * * * * * Loglo
G.debug ("Trying to find [" + Resource + "] using a context ClassLoader" + ClassLoader + ".");
* * * * * * * * * * URL = classloader.getresource (Resource);         /*/if (URL!= null) {/* * */return URL; */* * * * * */
* * * * * * * * * * * *///////* ClassLoader = Loader.class.getClassLoader (); */if (ClassLoader!= null) {/* 108/Loglog.debug ("trying to find [" + Resource + "] using" + CLA
Ssloader + "class loader.");
* *////* * * URL = classloader.getresource (Resource);
*/if (URL!= null)/* 112/return URL;  /*   * * * * * * * * * * * * */* * * * * * * * 116/Loglog.warn ("Caught Exception while in L
Oader.getresource. This is innocuous. ", t); */*//////* 123/* Loglog.debug ("trying to find [" + Resource + "] using Classloader.getsystemresour
CE (). ");
* */////* * Classloader.getsystemresource (Resource); /*     */   }

The load order can be seen from the debug information:

1 trying to find ["+ Resource +"] using the context ClassLoader "+ ClassLoader +". " Load the configuration file in this project

2 trying to find ["+ Resource +"] using "+ ClassLoader +" class loader. To load a configuration file in a jar package

3 trying to find ["+ Resource +"] using Classloader.getsystemresource (). Load the configuration file for the System class path


PS: In fact, log4j is really fierce, he also supports the asynchronous write log.

can refer to http://raymondhekk.iteye.com/blog/229937
log4j Log asynchrony dramatically improves system performance to understand

Refer to the simple introduction of the 1 http://topic.csdn.net/u/20101123/09/a697e3c5-2142-4597-91b8-5fad66da24bb.html log4j

          2  http://www.iteye.com/topic/378077         The main reference for this article

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.