commons-logging的使用
来源:互联网 发布:gopro软件中文版 编辑:程序博客网 时间:2024/05/29 04:39
- 简介
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>测试代码如下:
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。阅读全文
0 0
- Commons-Logging的使用
- commons-logging的使用
- commons-logging的使用
- 使用commons-logging的笔记
- commons-logging的使用简介
- Commons Logging与Log4j的使用心得
- commons-logging-Log和Log4j的使用
- java中commons-logging的使用
- commons logging 的 使用方法
- Commons Logging 的使用方法
- Commons Logging 的使用方法
- Apache Commons-logging使用实例
- Apache Commons-logging使用实例
- Apache Commons-logging使用实例
- Apache Commons-logging使用实例
- log4j+commons-logging结合使用
- Apache Commons-logging使用实例
- Apache Commons-logging使用实例
- 第5周项目3-括号的匹配
- n个人中每m个出队一次
- 用户 角色 权限 菜单
- LeetCode:Palindrome Number
- iOS 快捷键~新文件、编辑、调试
- commons-logging的使用
- 【codevs 1081】线段树练习 2
- 3D自动旋转图(HTML5)
- @RequestMapping请求方式发生冲突时候的情况
- 微积分小糊涂,国庆节大快乐!
- Struts2中ActionContext、 ServletActionContext详解
- canvas 绘制八卦图
- 个人Blog开发学习笔记
- AtCoder Grand Contest 001 C