首页 > 其他 > 详细

log4j默认加载的配置文件

时间:2017-07-31 23:51:14      阅读:546      评论:0      收藏:0      [点我收藏+]

通过查看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默认加载的配置文件

原文:http://www.cnblogs.com/joyvon/p/7266256.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!