一介绍
The Apache Commons Logging (JCL) provides a Log interface that is intended to be both light-weight and an independent abstraction of other logging toolkits.It provides the middleware/tooling developer with a simple logging abstraction, that allows the user (application developer) to plug in a specific logging implementation. JCL provides thin-wrapper Log implementations for other logging tools, including Log4J,Avalon LogKit (the Avalon Framework's logging infrastructure), JDK 1.4, and an implementation of JDK 1.4 logging APIs (JSR-47) for pre-1.4 systems. The interface maps closely to Log4J and LogKit. Familiarity with high-level details of the relevant Logging implementations is presumed.
JCL组件提供了一个日志的(Log)接口,它是一个轻量级的日志工具,并且不依赖于具体的日志实现工具。它向中间件或日志工具的开发者提供了一个简单的日志操作抽象,同时也允许程序开发人员使用不同的具体日志实现工具,允许用户随时使用不同的日志实现.
JCL提供接口的同时,还对其他一些日志工具,包括Log4j,Avalon LogKit和jdk1.4等进行了简单的包装。此接口更接近于Log4j和LogKit的实现.
二.LogFactory的实例化
从应用观点,首先需要的就是装载一个引用LogFactory实例的对象以便为这个应用创建一个Log实例。这通常通过调用静态的getFactory()方法完成。这个方法实现了如下的发现 算法来选择LogFactory实现类的名字并在应用中使用它:
1.检查org.apache.commons.logging.LogFactory的系统属性。
2. 使用JDK 1.3 JAR服务发现机制(参见 http://java.sun.com/j2se/1.3/docs/guide/jar/jar.html获得更多信息)来查找名为META- INF/services/org.apache.commons.logging.LogFactory的资源,其中第一行既包含了需要的类名。
3.在应用程序的classpath中查找名为common-logging.properties的属性文件,其中的org.apache.commons.logging.LogFactory属性定义了期望的实现类的名字。
4.回到到默认的实现中(org.apache.commons.logging.impl.LogFactoryImpl类)
LogFactory的实现类的主要目的是通过调用getInstance()方法创建(如果需要)并返回一个Log实例。默认实现使用如下规则:至多只有一个同名的Log实例被创建。以后的使用相同名字或类参数的getInstance()方法都将调用同一个LogFactory实例,并将返回同一个Log实例。
三.具体日志工具的实例化(默认情况下调用org.apache.commons.logging.impl.LogFactoryImpl.getInstance()进行实例化)
There are two base abstractions used by JCL: Log (the basic logger) and LogFactory (which knows how to create Log instances).Specifying a particular Log implementation is very useful (whether that is one provided by commons-logging or a user-defined one).Specifying a LogFactory implementation other than the default is a subject for advanced users only, so will not be addressed here. The default LogFactory implementation uses .the following discovery process to determine what type of Log implementation it should use (the process terminates when the first positive match - in order - is found):
1.Look for a configuration attribute of this factory named org.apache.commons.logging.Log .
2.Look for a system property named org.apache.commons.logging.Log.
3.If the Log4J logging system is available in the application class path, use the corresponding wrapper class (Log4JLogger).
4.If the application is executing on a JDK 1.4 system, use the corresponding wrapper class (Jdk14Logger).
5.Fall back to the default simple logging wrapper (SimpleLog).
使用Commons Logging时,它会自动寻找系统中存在的日志工具,并自行合理的设置,用户一般情况下不需要进行任何的设置工作.
JCL有两个基本的抽象类:Log(基本记录器)和LogFactory(负责创建Log实例)。那么LogFactory是如何在系统中寻找日志工具呢?它是按照一定的顺序去寻找日志工具的。在这个步骤中,它会将第一个找到的日志工具作为当前系统的日志工具。其具体的寻找顺序如下:
1.寻找当前factory中org.apache.commons.logging.Log属性所指定的值(commons-logging.properties).
2.寻找系统属性org.apache.commons.logging.Log所指定的值(System.getProperty()).
3.如果应用程序的Classpath中有Log4j,则使用相关的包装(wrapper)类(Log4JLogger).
4.使用默认的简单日志包装类(SimpleLog).
四.在开发中使用logging
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFacory;
public class Test{
Log log = LogFactory.getLog(Test.calss);
//或者
//Log log = LogFactory.getLog("Test");
.....
}