LogFactory factory = getCachedFactory(contextClassLoader); if (factory != null) { return factory; } //..... factory = (LogFactory) factories.get(contextClassLoader);
否则
0准备:读取commons-logging.properties ,将 thisClassLoader = getClassLoader(LogFactory.class); 作为baseClassLoader。baseClassLoader = thisClassLoader;
1. 读取global system property 配置
//org.apache.commons.logging.LogFactory String factoryClass = getSystemProperty(FACTORY_PROPERTY, null); factory = newFactory(factoryClass, baseClassLoader, contextClassLoader);2. 如空,利用JDK1.3 开始提供的service 发现机制,扫描classpah 下的META-INF/services/org.apache.commons.logging.LogFactory 文件
InputStream is = getResourceAsStream(contextClassLoader,SERVICE_ID); rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); String factoryClassName = rd.readLine(); //获得factoryClassName factory = newFactory(factoryClassName, baseClassLoader, contextClassLoader );3. 如空,读取0步骤已经读入的commons-logging.properties文件
String factoryClass = props.getProperty(FACTORY_PROPERTY); factory = newFactory(factoryClass, baseClassLoader, contextClassLoader);
4. 如空,则采用默认的logFactory:org.apache.commons.logging.impl.LogFactoryImpl
factory = newFactory(FACTORY_DEFAULT, thisClassLoader, contextClassLoader);
善后: 1. 将factory保存到Hashtable中,不用再重新创建了
cacheFactory(contextClassLoader, factory);2. 设置factory的属性
if( props!=null ) { Enumeration names = props.propertyNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); String value = props.getProperty(name); factory.setAttribute(name, value); } }
========================
和log4j的集成
1. 配置commons-logging.properties文件为:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger2. 不配文件
if (logConstructor == null) { discoverLogImplementation(getClass().getName()); }而在discoverLogImplementation()中会创建该LogFactory
for(int i=0; (i<classesToDiscover.length) && (result == null); ++i) { result = createLogFromClass(classesToDiscover[i], logCategory, true); }classesToDiscover的定义为
private static final String[] classesToDiscover = { LOGGING_IMPL_LOG4J_LOGGER, "org.apache.commons.logging.impl.Jdk14Logger", "org.apache.commons.logging.impl.Jdk13LumberjackLogger", "org.apache.commons.logging.impl.SimpleLog" };可以看出,创建logFactory的优先级为log4j-->Jdk14Logger-->Jdk13LumberjackLogger-->SimpleLog