日志工具的使用

  slf4j----The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.

  以上是slf4j的官方文档的简单说明,既,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统。

  作为java初学者,看到MyEclipse的console里滚出来的各种复杂的信息一定很发愁,想要显示我们真正需要的信息怎么办?你想拥有自己喜欢的日志系统吗?那就来了解一下slf4j吧。

  他的使用很简单:

  我们熟知的日志系统有以下两种,slf4j也支持这两种。以下以log4j为例,看看怎么使用slf4j吧。

  1、JDK  logging-----logging.properties

  2、Log4j-----------log4j.properties

  第一步,添加jar包:

  log4j-1.2.15.jar

  slf4j-api-1.6.1.jar

  slf4j-log4j12-1.6.1.jar

  第二步,修改配置文件:

  log4j.properties

log4j.rootLogger=error, stdout

log4j.log.cn.oa2014.oa=error

   信息分为:

  debug 调试信息  (都显示)

  info 一般信息

  warn警告

  error错误

  fatal 严重错误信息

  测试:

  

 1 package cn.oa2014.oa.test;
 2 
 3 import org.apache.commons.logging.Log;
 4 import org.apache.commons.logging.LogFactory;
 5 import org.junit.Test;
 6 
 7 
 8 
 9 public class logTest {
10     
11     private Log log=LogFactory.getLog(this.getClass());
12     
13     
14     @Test
15     public void test() throws Exception {
16         log.debug(" this is a debug from jyh's test");
17         log.info("this is a info from jyh's test");
18         log.warn("this is a warn from jyh's test");
19         log.error("this is a error from jyh's test");
20         log.fatal("this is a fatal from jyh's test");
21     }
22 }

测试结果  :

19:52:29,406 ERROR logTest:19 - this is a error from jyh's test
19:52:29,409 FATAL logTest:20 - this is a fatal from jyh's test

 如上可知,如果把log4j.log.cn.oa2014.oa=error设置成了error就会只显示error和fatal信息了。这样你的日志信息就一目了然了。

  

原文地址:https://www.cnblogs.com/jyh317/p/3722163.html