一.Apache Commons Logging
???? http://zy19982004.iteye.com/blog/1867448里提到了commons-logging的工作方式,本文将看下代码怎么实现这一工作方式。
?
二.Apache Commons Logging类结构
???? 就这么简单,一个接口包,一个实现包。
?
三.类说明
- Log:A simple logging interface abstracting logging APIs,和JDBC API是一个性质的东西
- Level。对六种日志级别的操作,官网建议我们这样用:
http://commons.apache.org/proper/commons-logging/guide.html 写道It is important to ensure that log message are appropriate in content and severity. The following guidelines are suggested:?
fatal - Severe errors that cause premature termination. Expect these to be immediately visible on a status console. See also Internationalization.
error - Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console. See also Internationalization.
warn - Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console. See also Internationalization.
info - Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum. See also Internationalization.
debug - detailed information on the flow through the system. Expect these to be written to logs only.
trace - more detailed information. Expect these to be written to logs only. - LogFactory:抽闲类,获得Log类。?getFactory()先从缓存里取LogFactory,取不到再去找,然后放进缓存。其中找的这段代码如下
public static final String FACTORY_PROPERTY = "org.apache.commons.logging.LogFactory" protected static final String SERVICE_ID = "META-INF/services/org.apache.commons.logging.LogFactory" public static final String FACTORY_PROPERTIES = "commons-logging.properties" public static final String FACTORY_DEFAULT = "org.apache.commons.logging.impl.LogFactoryImpl" public static LogFactory getFactory() throws LogConfigurationException { //1.系统环境变量里是否定义了LogFactory实现类,System.getProperty(FACTORY_PROPERTY) //2.利用JDK1.3 开始提供的service 发现机制,会扫描classpah 下的SERVICE_ID文件,若找到则装载里面的配置,使用里面的配置 //3.Classpath下有FACTORY_PROPERTIES文件的话,看此properties里有无定义FACTORY_PROPERTY //4.如果123找不到LogFactory实现类,使用FACTORY_DEFAULT }
??public abstract Log getInstance(String name) throws LogConfigurationException; public abstract Log getInstance(Class clazz) throws LogConfigurationException; public static Log getLog(String name) throws LogConfigurationException { return (getFactory().getInstance(name)); } public static Log getLog(Class clazz) throws LogConfigurationException { return (getFactory().getInstance(clazz)); }
?? - LogFactoryImpl,实现LogFactory,真正产生Log类的地方。首先从缓存里取Log,取不到就按照http://zy19982004.iteye.com/blog/1867448 中的顺序加载Log类。(i<