现在的位置: 首页 > 综合 > 正文

关于 log4j.jar 和 commons-logging.jar 的一些记录

2013年09月07日 ⁄ 综合 ⁄ 共 2097字 ⁄ 字号 评论关闭

最近,项目发布在websphere里,经常出现log无法产生的问题,上网查询了一些关于log4j和commons-logging的一些信息,现在把它记录在下面:

      首先,讲一下这2个包的由来,log4j是大家很熟悉的apache用来在项目中记录log的一个常用的包,那为什么apache为什么还要出一个commons-logging的包呢?我认为是apache多事了,做的挺多余,原因是由于,JDK1.4以后在 JDK 里面提供了一个log(java.util.logging.Logger),所以apache又用commons-logging把他封装了,提供了一个实现(org.apache.commons.logging.impl.Jdk14Logge)。

     那项目里,这2个包(log4j.jar 和 commons-logging.jar )都丢进去,他内部是怎么处理呢?下面是apache文挡里的原话:

  1. Look for a configuration attribute of this factory namedorg.apache.commons.logging.Log(for backwards compatibility to pre-1.0 versions of this API, an attributeorg.apache.commons.logging.logis 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 aprioritykey, 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 namedorg.apache.commons.logging.Log(for backwards compatibility to pre-1.0 versions of this API, a system propertyorg.apache.commons.logging.logis 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).

    所以,从上来看下来,commons-logging的最大作用就是,无论如何,你只要丢了这个进去,保证项目是一定有Log的实现类,Log一定会被记录下来。(先找配置有没有定义,没有,继续找,有没有Log4j.还没有,是不是1.4JDK,还不是,用SimpleLog)。

    但是这样,发布在WebSphere里,由于WebSphere本身也采用了commons-logging,会和自己的项目有时有有冲突,关于解决办法,网上一大堆, IBM还专门出文档来解决这个问题。

    所以,我认为,我们开发的时候就不要用commons-logging了,指明只用log4J不就ok了,我们开发的时候,一定是会要求有Log出来的,没有必要要让commons-logging这个东西来保证一定有Log的实现类,Log就是由Log4J出来,不要让Classloader去东找西找了,想在WS里理清楚 它的ClassLoader太浪费时间了。

转自:http://squll.blogbus.com/logs/47802699.html

抱歉!评论已关闭.