<< Hibernate optimistic locking exception | 首页 | 一个在Spring里使用quartz完成时序调度工作的例子 >>

Commons Logging自动发现日志工具过程和Log4j的初始化

Jakarta Commons Logging (JCL)提供了统一的日志接口,它目的是成为轻量级和不依赖其他日志工具的抽象层。它把一个简单的日志抽象提供给二次开发者,它允许用户插入一个指定的日志实现。
JCL为其他日志工具提供了一个代理日志实现,包括Log4JAvalon LogKit,JDK 1.4,以及JDK1.4日志API的实现。这个接口类似的映射到Log4j和LogKit。

下面是Commons Logging  (JCL)自动地发现选择一个日志实现工具的过程:

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 (for backwards compatibility to pre-1.0 versions of this API, an attribute org.apache.commons.logging.log is also consulted).

    Configuration attributes can be set explicitly by java code, but they are more commonly set by placing a file named commons-logging.properties in the classpath. When such a file exists, every entry in the properties file becomes an "attribute" of the LogFactory. When there is more than one such file in the classpath, releases of commons-logging prior to 1.1 simply use the first one found. From release 1.1, each file may define a priority key, and the file with the highest priority is used (no priority definition implies priority of zero). When multiple files have the same priority, the first one found is used.

    Defining this property in a commons-logging.properties file is the recommended way of explicitly selecting a Log implementation.

  2. Look for a system property named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, a system property org.apache.commons.logging.log is also consulted).
  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).

Consult the JCL javadocs for details of the various Log implementations that ship with the component. (The discovery process is also covered in more detail there.)

Log4j是项目中常用的日志工具,了解它的初始化过程也是需要的,下面是Log4j的默认的初始化过程:

The default initialization is very useful in environments where the exact entry point to the application depends on the runtime environment. For example, the same application can be used as a stand-alone application, as an applet, or as a servlet under the control of a web-server.

The exact default initialization algorithm is defined as follows:

  1. Setting the log4j.defaultInitOverride system property to any other value then "false" will cause log4j to skip the default initialization procedure (this procedure).
  2. Set the resource string variable to the value of the log4j.configuration system property. The preferred way to specify the default initialization file is through the log4j.configuration system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value "log4j.properties".
  3. Attempt to convert the resource variable to a URL.
  4. If the resource variable cannot be converted to a URL, for example due to a MalformedURLException, then search for the resource from the classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) which returns a URL. Note that the string "log4j.properties" constitutes a malformed URL.

See Loader.getResource(java.lang.String) for the list of searched locations.

  1. If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL.

The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in which case the DOMConfigurator will be used. You can optionaly specify a custom configurator. The value of the log4j.configuratorClass system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement the Configurator interface.

 

标签 :



发表评论 发送引用通报