Logger.getLogger与LogFactory.getLog

Logger来自log4j自己的包。如果用Logger.getLogger,需要一个log4j的jar包,用此方式你只能依靠log4j: 

LogFactory来自common-logging包。如果用LogFactory.getLog,你可以用任何实现了通用日志接口的日志记录器替换log4j,而程序不受影响。apache的common-logging包是通用日志接口,通过这个中间层,你可以随便指定到底用哪个日志系统。增加系统的灵活性。若log4j它不存在, commons-logging 会另行选择其它的日志实现类。 这样保证了程序中不一定要使用log4j这个日志文件了 。

增强灵活性的理由: 
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 。

为了简化配置 commons-logging ,一般不使用 commons-logging 的配置文件,也不设置与 commons-logging 相关的系统环境变量,而只需将 Log4j 的 Jar 包放置到 classpash 中就可以了。这样就很简单地完成了 commons-logging 与 Log4j 的融合。 


基于common-logging的运行方式: 

package org; 
import org.apache.commons.logging.Log; 
import org.apache.log4j.Logger; 
public class Test extends TagSupport{ 
public static Log log=LogFactory.getLog(Test.class); 
public static void test() 
{ 
log.debug("111"); 
log.info("125"); 
log.warn("485"); 
log.error("error"); 

} 
public static void main(String[] a) 
{ 
Test.test(); 
} 
} 

基于log4j的运行方式 :
import org.apache.log4j.Logger; 
import org.apache.log4j.PropertyConfigurator; 
public class TestLog4j { 

static Logger logger = Logger.getLogger(TestLog4j.class); 
   public static void main(String args[]) { 
   PropertyConfigurator.configure("log4j.properties"); 
   logger.debug("Here is some DEBUG"); 
   logger.info("Here is some INFO"); 
      logger.warn("Here is some WARN"); 
      logger.error("Here is some ERROR"); 
      logger.fatal("Here is some FATAL"); 
   } 
} 

commons-logging 仅仅对 Log4j( 当然还包括其它 LOG 实现 ) 作了一层包装,具体的日志输出还是在内部转交给身后的 Log4j 来处理,还有log4j会默认的去classes目录下去寻找log4j.properties 文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值