一.Apache Log4j
- http://logging.apache.org/log4j/2.x/
- 提供了全面的日志管理。
二.Apache commons-logging
- http://commons.apache.org/proper/commons-logging/
- there are many logging implementations out there,The Logging package is an ultra-thin bridge between different logging implementations...
- 翻译过来:
- commons-logging是一个简单的适配器,为各种各样的日志实现提供了统一的接口。
- 当变化日志实现时,application不需要做任何改变。
- commons-logging也提供了简单的日志实现,但不推荐使用。
三.commons-logging怎样适配到合适的日志系统
1) 首先在classpath下寻找自己的配置文件commons-logging.properties,如果找到,则使用其中定义的Log实现类;
2) 如果找不到commons-logging.properties文件,则在查找是否已定义系统环境变量org.apache.commons.logging.Log,找到则使用其定义的Log实现类;
3) 否则,查看classpath中是否有Log4j的包,如果发现,则自动使用Log4j作为日志实现类;
4) 否则,使用JDK自身的日志实现类(JDK1.4以后才有日志实现类);
5) 否则,使用commons-logging自己提供的一个简单的日志实现类SimpleLog;
四.使用Log4j和commons-logging
- 项目里加入log4j.jar和commons-logging.jar,加入classpath下。
- 新建log4j.properties,加入classpath下。
-
- package com.joyoungzhang.log4j;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class Log4jModel {
- private static final Log LOG = LogFactory.getLog(Log4jModel.class);
- public static void main(String[] args) {
- if (LOG.isErrorEnabled()) {
- LOG.error("error......");
- }
- if (LOG.isInfoEnabled()) {
- LOG.info("info......");
- }
- if (LOG.isDebugEnabled()) {
- LOG.debug("debug......");
- }
- }
- }
五.单独使用Log4j或commons-logging
- 单独使用Log4j,在编程复杂度上比一起使用Log4j和commons-logging更低,但整个系统与Log4j是耦合的,有一天我不想使用Log4j了,整个系统都得改。
- 单独使用commons-logging当然也可以,但commons-logging的作用体现在“为所有的日志实现提供统一的接口”,本身并没有强大的日志实现系统,所以也不推荐。
六.你也可以阅读以下文档