DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

SLF4J 教程(自由在各种log中切换)

一、介绍:
简单日记门面(simple logging Facade for java)SLF4J是为各种loging APIs提供一个简单统一的
接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现。 Logging API实现既可以
选择直接实现SLF4J接的loging APIs如: NLOG4J、SimpleLogger。也可以通过SLF4J提供的API实现
来开发相应的适配器如Log4jLoggerAdapter、JDK14LoggerAdapter。在SLF4J发行版本中包含了几个
jar包,如slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-log4j13.jar,
slf4j-jdk14.jar and slf4j-jcl.jar通过这些jar文件可以使编译期与具体的实现脱离。或者说可以
灵活的切换
二、官方站点
官方的网站:http://www.slf4j.org/manual.html
三、为何使用slf4j?
我们在开发过程中可能使用各种log,每个Log有不同的风格、布局,如果想灵活的切换那么slf4j是比较好的
选择。
四、如何使用slf4j
下边一段程序是经典的使用slf4j的方法.

 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Wombat {
    
final Logger logger = LoggerFactory.getLogger(Wombat.class);
    Integer t;
    Integer oldT;
    
public void setTemperature(Integer temperature) {
        oldT 
= t;
        t 
= temperature;
        logger.error(
"Temperature set to {}. Old temperature was {}.", t, oldT);
        
if (temperature.intValue() > 50{
            logger.info(
"Temperature has risen above 50 degrees.");
        }

    }

    
public static void main(String[] args) {
        Wombat wombat 
= new Wombat();
        wombat.setTemperature(
1);
        wombat.setTemperature(
55);
    }

}


下边介绍一下运行上边程序的过程。
1,编译上边的程序,需要classpath中加入slf4j-api-1.4.1.jar文件
2,运行时,需要classpath中加上slf4j-simple-1.4.1.jar
运行得到结果:
----------------------------
0 [main] ERROR Wombat - Temperature set to 1. Old temperature was null.
0 [main] ERROR Wombat - Temperature set to 55. Old temperature was 1.
0 [main] INFO Wombat - Temperature has risen above 50 degrees.
这个是simple log风格,

3,切换:如果想切换到jdk14的log的风格,只需要把slf4j-simple-1.4.1.jar
从classpath中移除,同时classpath中加入slj4j-jdk14-1.4.1.jar
这时的运行结果:
---------------------------------------------------
2007-7-9 10:40:15 Wombat setTemperature
严重: Temperature set to 1. Old temperature was null.
2007-7-9 10:40:16 Wombat setTemperature
严重: Temperature set to 55. Old temperature was 1.
2007-7-9 10:40:16 Wombat setTemperature
信息: Temperature has risen above 50 degrees.
已经变成jdk14的log风格了。
4,再次切换到log4j
同样移除slj4j-jdk14-1.4.1.jar,加入slf4j-log4j12-1.4.1.jar,同时加入log4j-1.2.x.jar
加入log4j.properties。得到显示结果:
---------------------------------------
10:42:27,328 ERROR Wombat: Temperature set to 1. Old temperature was null.
10:42:27,328 ERROR Wombat: Temperature set to 55. Old temperature was 1.
10:42:27,328  INFO Wombat: Temperature has risen above 50 degrees.
在不同的风格中切换只需要在部署期切换类库就可以了,和开发时无关。

posted on 2007-07-09 10:47 dreamstone 阅读(18161) 评论(9)  编辑  收藏 所属分类: 利器其它开源框架

评论

# re: SLF4J 教程(自由在各种log中切换) 2007-07-09 12:40 Unmi

commons-logging本来就是一个通用框架,其实也很方便的,没见这个有什么优势啊  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换) 2007-07-09 12:46 dreamstone

每个人、每个公司的习惯,倾向不同。有log4j ,jdk log ,NLOG4J等就说明大家有不同的需求,呵呵。优势在于整合。

  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换) 2007-07-09 13:31 lingo

不懂,跟commons-logging比有什么有事吗?虽然commons-logging是个桥接器,不过实际上一直在用的还是log4j  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换) 2007-07-09 22:05 se

不错  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换) 2007-07-10 14:19 Roy

不同在这里
logger.error("Temperature set to {}. Old temperature was {}.", t, oldT);

这是其他Logger不支持的功能。
以前我们都要这样写
logger.error("Temperature set to "+t +". Old temperature was "+oldT+"." );

也就是经常说为什么要加上log.isDebugalbe()判断的原因之一,这样做会减少字段串的合并,理解上减少JVM垃圾  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换) 2007-09-24 15:14 hanfeng

SLF4J的作者就是Log4j的作者,他正在开发logback来代替log4j,logback有更高的性能。logback支持上面提到的
logger.error("Temperature set to {}. Old temperature was {}.", t, oldT);
commons-logging没有提供类似的接口,SLF4J提供了,而且解决了classloader的问题。  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换) 2011-04-11 20:07 Jacklondon Chen

@hanfeng
我不觉得 slf4j 解决了 classloader 问题。
commons-logging 也号称 解决了 classloader 问题。但是当 J2EE server 也用commons-logging 并且版本比你的应用中所用commons-logging 版本老时,问题就出现了。
现在 slf4j 没有这个问题,是因为 J2EE server 不用 slf4j. 而 classloader 问题的最终解决,需要 JDK、J2EE server 两方面的厂商合作出一个新的解决办法,才能搞定问题。现在还早呢。
----欢迎大家试用我们的单点登录系统 http://zhegui.biz
  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换)[未登录] 2014-04-18 13:27 Robot

common-logging通过动态查找的机制,在程序运行时自动找出真正使用的日志库。由于它使用了ClassLoader寻找和载入底层的日志库, 导致了象OSGI这样的框架无法正常工作,因为OSGI的不同的插件使用自己的ClassLoader。 OSGI的这种机制保证了插件互相独立,然而却使Apache Common-Logging无法工作。

slf4j在编译时静态绑定真正的Log库,因此可以再OSGI中使用。另外,SLF4J 支持参数化的log字符串,避免了之前为了减少字符串拼接的性能损耗而不得不写的if(logger.isDebugEnable()),现在你可以直接写:logger.debug(“current user is: {}”, user)。拼装消息被推迟到了它能够确定是不是要显示这条消息的时候,但是获取参数的代价并没有幸免。  回复  更多评论   

# re: SLF4J 教程(自由在各种log中切换)[未登录] 2016-03-22 17:24 ddd

ewrwrw  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: