zoukankan      html  css  js  c++  java
  • commons-logging的使用

    • 简介

    commons-logging是Apache commons类库中的一员。Apache commons类库是一个通用的类库,提供了基础的功能,比如说commons-fileupload,commons-httpclient,commons-io,commons-codes等。
    commons-logging能够选择使用Log4j还是JDK Logging,但是他不依赖Log4j,JDK Logging的API。如果项目的classpath中包含了log4j的类库,就会使用log4j,否则就使用JDK Logging。使用commons-logging能够灵活的选择使用那些日志方式,而且不需要修改源代码。

     

    • 使用commons-logging的一个例子

    commons-logging的使用类似于Log4j,他们的级别以及使用规则是完全一样的。下面来一个demo:
    首先我们要在项目中添加commons-logging的maven依赖:

    [html] view plain copy
     
    1. <dependency>  
    2.             <groupId>commons-logging</groupId>  
    3.             <artifactId>commons-logging</artifactId>  
    4.             <version>1.2</version>  
    5.         </dependency>  

    测试代码如下:

    [java] view plain copy
     
    1. package org.linkinpark.commons.commonslogging;  
    2.   
    3.   
    4. import org.apache.commons.logging.Log;  
    5. import org.apache.commons.logging.LogFactory;  
    6. import org.junit.Test;  
    7.   
    8.   
    9. /** 
    10.  * @创建作者: LinkinPark 
    11.  * @创建时间: 2016年2月26日 
    12.  * @功能描述: commons-logging的测试类 
    13.  */  
    14. public class CommonsLoggingTest  
    15. {  
    16.     public static Log LOG = LogFactory.getLog(CommonsLoggingTest.class);  
    17.   
    18.   
    19.     @Test  
    20.     public void test()  
    21.     {  
    22.         LOG.debug("debug()...");  
    23.         LOG.info("info()...");  
    24.         LOG.error("error()...");  
    25.     }  
    26.   
    27.   
    28. }  

    1,现在我们在项目中不要添加log4j的依赖,看下效果。

    运行上面的测试,junit绿条,然后控制台输出如下:

    [plain] view plain copy
     
    1. 二月 26, 2016 10:34:23 上午 org.linkinpark.commons.commonslogging.CommonsLoggingTest test  
    2. 信息: info()...  
    3. 二月 26, 2016 10:34:23 上午 org.linkinpark.commons.commonslogging.CommonsLoggingTest test  
    4. 严重: error()...  

    前面我也说过了,JDK自带的Logging其实是一个鸡肋,竟然没有debug的日志级别,差评。。。

    2,现在我们在项目中添加log4j的依赖,看下效果。

    只要我们在项目中添加了log4j的jar包,那么commons-logging就会自动切到log4j的日志输出。所以现在我们不提供log4j.properties配置文件,所以控制台输出如下:

    [plain] view plain copy
     
    1. log4j:WARN No appenders could be found for logger (org.linkinpark.commons.commonslogging.CommonsLoggingTest).  
    2. log4j:WARN Please initialize the log4j system properly.  
    3. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.  

    现在我们添加log4j.properties配置文件:

    [html] view plain copy
     
    1. log4j.rootLogger=DEBUG,console  
    2.   
    3. # 以下是rootLogger的配置,子类默认继承,但是子类重写下面配置=rootLogger+自己配置,我晕  
    4. #输出到控制台     
    5. log4j.appender.console=org.apache.log4j.ConsoleAppender    
    6. #设置输出样式     
    7. log4j.appender.console.layout=org.apache.log4j.PatternLayout   
    8. #日志输出信息格式为  
    9. log4j.appender.console.layout.ConversionPattern=[%-d{yyyy-MM-dd HH:mm:ss}]-[%t-%5p]-[%C-%M(%L)]: %m%n   

    再次运行测试,junit绿条,然后控制台正常输出日志:

    [plain] view plain copy
     
    1. [2016-02-26 10:47:13]-[main-DEBUG]-[org.linkinpark.commons.commonslogging.CommonsLoggingTest-test(19)]: debug()...  
    2.  [2016-02-26 10:47:13]-[main- INFO]-[org.linkinpark.commons.commonslogging.CommonsLoggingTest-test(20)]: info()...  
    3.  [2016-02-26 10:47:13]-[main-ERROR]-[org.linkinpark.commons.commonslogging.CommonsLoggingTest-test(21)]: error()...  
    4.    

    3,显示配置commons-logging启用log4j

    默认的,common-logging会自动检查是否使用log4j,也可以使用配置文件显示的启用log4j。配置文件为commons-logging.properties,放在程序的classpath下即可。
    例如:

    [html] view plain copy
     
    1. org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4J-Logger  
    2. org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl  

    关于上面的这种配置了解下就OK了,比较约定优于配置,commons-logging已经支持自动扫描了,我们就不需要人为的添加这些无聊的配置文件了。

     

    • 总结

    严格的说,commons-logging不是一个日志控件,没有日志功能,它只是统一了JDK Logging与Log4j的API,并把日志功能交给JDK Loggings或者是log4j。对于不能确定日志方式的系统,commons-logging是一个不错的选择,Spring,Hibernate,Struts等使用的都是commons-logging。下一篇我们会研究下Commons-logging的源码,来深入的整理下Commons-logging。


    如果有Log4j,commons-logging会把输出原封不动的交给log4j。如果没有log4j,commons-logging会将相应的输出转化成JDK Logging的输出。

  • 相关阅读:
    弹出层
    jQuerySelectors(选择器)的使用(三、简单篇)
    jQuerySelectors(选择器)的使用(二、层次篇)
    jQuerySelectors(选择器)的使用(四五、内容篇&可见性篇)
    对frameset、frame、iframe的js操作
    文件上传
    C# 字符串操作
    图片防盗链之HttpHandler方法实现
    MSSQL 存储过程
    dataset操作
  • 原文地址:https://www.cnblogs.com/pegasus827/p/9124778.html
Copyright © 2011-2022 走看看