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依赖:
    <dependency>
    			<groupId>commons-logging</groupId>
    			<artifactId>commons-logging</artifactId>
    			<version>1.2</version>
    		</dependency>
    测试代码如下:
    package org.linkinpark.commons.commonslogging;
    
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.junit.Test;
    
    
    /**
     * @创建作者: LinkinPark
     * @创建时间: 2016年2月26日
     * @功能描述: commons-logging的测试类
     */
    public class CommonsLoggingTest
    {
    	public static Log LOG = LogFactory.getLog(CommonsLoggingTest.class);
    
    
    	@Test
    	public void test()
    	{
    		LOG.debug("debug()...");
    		LOG.info("info()...");
    		LOG.error("error()...");
    	}
    
    
    }

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

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

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

    二月 26, 2016 10:34:23 上午 org.linkinpark.commons.commonslogging.CommonsLoggingTest test
    信息: info()...
    二月 26, 2016 10:34:23 上午 org.linkinpark.commons.commonslogging.CommonsLoggingTest test
    严重: error()...
    
    前面我也说过了,JDK自带的Logging其实是一个鸡肋,竟然没有debug的日志级别,差评。。。

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

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

    log4j:WARN No appenders could be found for logger (org.linkinpark.commons.commonslogging.CommonsLoggingTest).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    
    现在我们添加log4j.properties配置文件:

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

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

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


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

    默认的,common-logging会自动检查是否使用log4j,也可以使用配置文件显示的启用log4j。配置文件为commons-logging.properties,放在程序的classpath下即可。
    例如:
    org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4J-Logger
    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。


  • 相关阅读:
    个性化联邦学习算法框架发布,赋能AI药物研发
    ES入门 (2) 数据格式/类型
    ES入门 (1) 使用基础(1)安装(1) WIN 单机
    Java 之 JDBC:(十)Spring的JDBCTemplate
    Java 之 JDBC:(九)Apache-DBUtils实现CRUD操作
    Java 之 JDBC:(八)数据库连接池
    Java 之 JDBC:(七)DAO及相关实现类
    Java 之 JDBC:(六)数据库事务
    Java 之 JDBC:(五)批量插入
    第七节:循环结构
  • 原文地址:https://www.cnblogs.com/LinkinPark/p/5232836.html
Copyright © 2011-2022 走看看