1. 程式人生 > >log4j默認加載的配置文件

log4j默認加載的配置文件

nag 版本 url () fault 加載器 figure else pos

通過查看log4j源碼,版本1.2.16,log4j包 去默認的位置加載 配置文件;

文件名如下:

static public final String DEFAULT_CONFIGURATION_FILE = "log4j.properties";
  
  static final String DEFAULT_XML_CONFIGURATION_FILE = "log4j.xml";  

加載配置的過程:

LogManager 的 static 代碼段:

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."); }

Loader.getResource()代碼如下,其中getTCL(),獲取的是線程上下文類加載器,即{Thread.currentThread().getContextClassLoader()},該類加載器是可設置的,默認為系統類加載器;

 static public URL getResource(String resource) {
    ClassLoader classLoader = null;
    URL url = null;
    
    try {
      if(!java1 && !ignoreTCL) {
        classLoader = getTCL();
        if(classLoader != null) {
          LogLog.debug("Trying to find ["+resource+"] using context classloader "
               +classLoader+".");
          url = classLoader.getResource(resource);      
          if(url != null) {
            return url;
          }
        }
      }

log4j默認加載的配置文件