Yesterday on-line toss a half-day, found that the log is always unable to print correctly to the specified file, but Tomcat catalina.out print the business log, carefully check the online log4j configuration, no problems found, console log input is not open. Then quickly use the last online package to replace the re-release, found that the problem solved.
Taking into account the new on-line jar package that relies on other systems, after examining the jar, it is found that the jar package contains the log4j.xml!!
Why did log4j choose Log4j.xml in the jar instead of his own log4j.properties file, for the reason that it was in the Logmanager class of log4j?
See section Code:
/** * @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";
The above is a member variable of Logmanager.
Then look at the static module:
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_con Figuration_key, NULL); String configuratorclassname = Optionconverter.getsystemproperty (CONFIG Urator_class_key, NULL); URL url = null; If the user has not specified the Log4j.configuration//property, we search first for the file "Log4j.xml" and th En//"log4j. Properties "if (configurationoptionstr = = null) {URL = Loader.getresource (default_xml_configuration_file); if (url = = N ull) {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 class path URL = Loader.getresource (CONFIGURATIONOPTIONSTR); }}//If We have a non-null URL and then delegate the rest of the//configuration to the Optionconvert Er.selectandconfigure//method. if (URL! = null) {loglog.debug ("Using URL [" +url+ "] for automatic log4j configuration."); try {optionconverter.selectandconfigure (URL, Configuratorclassname, logmanager.getloggerrepository ()); } catch (Noclassdeffounderror e) {Loglog.warn ("Error during default initialization", E); }} else {loglog.debug ("Could not find resource: [" +configurationoptionstr+ "]."); }} else {Loglog.debug ("Default initialization of overridden by" + Default_init_override_key + "PR Operty. ");}}
The key point is
if (configurationoptionstr = = null) {URL = Loader.getresource (default_xml_configuration_file); if (url = = null) { URL = Loader.getresource (default_configuration_file);} }
Describes the configuration path that log4j first uses, and then uses the XML configuration file under the Classpath before selecting the properties file. This is the reason, I hope to help everyone.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Why the log4j configuration fails