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。
commons-logging的使用的更多相关文章
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...
- Spring 使用 SLF4J代替 Commons Logging 写日志 异常
项目的日志更换成slf4j和logback后,发现项目无法启动.错误提示 Caused by: java.lang.NoClassDefFoundError: Lorg/apache/commons/ ...
- org.apache.log4j与org.apache.commons.logging这两个包有什么区别
apache common logging是一种log的框架接口,它本身并不实现log记录的功能,而是在运行时动态查找目前存在的日志库,调用相关的日志函数,从而隐藏具体的日志实现log4j是具体的日志 ...
- 使用slf4j取代Apache Commons Logging
假如你正在开发应用程序所调用的组件当中已经使用了 JCL(之前叫 Jakarta Commons Logging,JCL) 的,还有一些组建可能直接调用了 java.util.logging,这时你需 ...
- com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException
在日志中, 查看导入的包是否是 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
- Taxonomy of class loader problems encountered when using Jakarta Commons Logging(转)
Acknowledgments I would like to thank Jacob Kjome for reviewing early drafts of this document. His c ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
1.错误叙述性说明 2014-7-12 0:38:57 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplica ...
- java.lang.ClassNotFoundException: org.apache.commons.logging.Log
严重: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catalin ...
- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方案
导入commons-logging-1.2.jar辅助类包即可. 报错提示: Exception in thread "main" java.lang.NoClassDefFoun ...
- 廖雪峰Java3异常处理-2断言和日志-3使用Commons Logging
Commons Logging是Apache创建的日志模块: 可以挂接不同的日志系统 可以通过配置文件指定挂接的日志系统 自动搜索并使用Log4j 如果Log4j不存在,使用JDK Logging(J ...
随机推荐
- android散点技术
1.怎么实现android虚拟按键(回退和回到主界面) android 中如何模拟back键 2.怎么用局域网的方式(wifi或是网线)来调试android机 参考资料: http://www.ith ...
- android开发遇到的问题
1.虚拟机运行出下面的错Failed to allocate memory: 8 Failed to allocate memory: 8This application has requested ...
- PE格式详解讲解1
这篇文章主要转载自小甲鱼的加密解密部分,然后补充加上我自己的少许内容,原文地址–>传送门 下面的内容主要是围绕这个图来进行 MS-DOS头部 这个头部是为了兼容早期的DOS系统,PE文件的第一个 ...
- zookeeper leader选举算法源码
服务器状态 在QuorumPeer中有定义,这个类是一个线程. LOOKING:寻找Leader状态.处于该状态时,它会认为当前集群中没有Leader,进入选举流程. FOLLOWING: LEADI ...
- input表单的type属性详解,不同type不同属性之间区别
目标:详解表单input标签type属性常用的属性值 一.input标签和它的type属性 PS:input 元素可以用来生成一个供用户输入数据的简单文本框. 在默认的情况下, 什么样的数据均可以输入 ...
- Nginx的虚拟服务器域名配置
虚拟服务器名(server name)是通过指令server_name来指定的.在< Nginx是如何处理Request的?>一节中,我们讲到nginx分两步来匹配过来的Request请求 ...
- 任务调度框架Quartz原理简介
[TOC] 第一章 Quartz 1.1 Quartz概念 Quartz是OpenSymphony开源组织的一个Java开源项目, 在2009被Terracotta收购.Quartz官网 1.2 Qu ...
- React Native学习(三)—— 使用导航器Navigation跳转页面
本文基于React Native 0.52 参考文档https://reactnavigation.org/docs/navigators/navigation-prop 一.基础 1.三种类型 Ta ...
- [搬运] C# 这些年来受欢迎的特性
原文地址:http://www.dotnetcurry.com/csharp/1411/csharp-favorite-features 在写这篇文章的时候,C# 已经有了 17 年的历史了,可以肯定 ...
- Entity Framework Core 懒加载
众所周知在EF 6 及以前的版本中,是支持懒加载(Lazy Loading)的,可惜在EF Core 并不支持,必须使用Include方法来支持导航属性的数据加载.不过现在EF Core的开发团队打算 ...