Have you noticed that you just need to put the log4j file under the classpath, it will automatically load, this is why.
Let's take a dip today.
Start with the Org.apache.log4j.LogManager, go into the class to see its code: it must have dawned on everyone.
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 the USEr has not specified the Log4j.configuration//property, we search a for the file "Log4j.xml" and then// ' 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 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+ "]."); }
}
}
Start with the code first:
/** Search for the properties file Log4j.properties in the CLASSPATH. */
Look at this sentence from the classpath to find log4j.properties files, the following are all around this sentence.
If there is no default init override and then get the resource
specified by the user or the default Config file.
1, if there is no rewrite of the default initialization, load this user-made log4j.properties or default configuration file.
If the user has not specified the Log4j.configuration
property, we search the file "Log4j.xml" and then
"Log4j.properties",
2, if the user does not develop log4j.configuration properties, the first load log4j.xml, if no further in the load log4j.properties
English proficiency is limited, it is roughly this meaning, we can discuss what is not clear to each other.
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. I can refer to another blog of my own loading.