昨天上线折腾了半天,发现日志始终不能正确打印到指定文件,反而Tomcat的catalina.out打印了业务日志,仔细检查线上log4j配置,未发现任何问题,console日志输入并没有打开。于是迅速使用上次上线包进行替换重新发布,发现问题解决。
考虑到本次上线新依赖了其他系统的jar包,检查该jar后发现,jar包中包含了Log4j.xml!!
log4j为何选择了jar中的log4j.xml而不是自己的log4j.properties文件呢,原因就在log4j的LogManager类中
看段代码:
/** * @deprecated This variable is for internal use only. It will * become package protected in future versions. * */ static public final String DEFAULT_CONFIGURATION_FILE = "log4j.properties"; static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml"; /** * @deprecated This variable is for internal use only. It will * become private in future versions. * */ static final public String DEFAULT_CONFIGURATION_KEY="log4j.configuration";
以上是LogManager的成员变量。
然后看static模块:
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); URL url = null; // if the user has not specified the log4j.configuration // property, we search first 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 is not a URL: // attempt to get the resource from the class path url = Loader.getResource(configurationOptionStr); } } // If we have a non-null url, then delegate the rest of the // configuration to the OptionConverter.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 + "property."); } }
关键点就在
if(configurationOptionStr == null) { url = Loader.getResource(DEFAULT_XML_CONFIGURATION_FILE); if(url == null) { url = Loader.getResource(DEFAULT_CONFIGURATION_FILE); } }
说明log4j优先使用程序制定的配置路径,然后使用类路径下的xml配置文件,然后才选择properties文件。这就是原因,希望对大家有所帮助。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 06:13:24