http://www.tuicool.com/articles/IfeUfq
 

slf4j(simple logging facade for java)是Java的简单的日志门面,它 不是具体的日志解决方案,它只服务于各种各样 slf4j-logo

的 日志系统。这里的slf4j-logo可以是log4j,可以是jdk的日志,可以是logback,还可以是slf4j-simple等 等。按照官方的说法,SLF4J是一个用于日志系统的简单Facade,允许最终用户在部署其应用时使用其所希望的日志系统。这里其实用到了一种设计模式 (Facade设计模式,门面设计模式)。如下图:

实际上,SLF4J所提供的核心API是一些接口以及一个LoggerFactory的工厂类。而在使用SLF4J的时候,不需要在 代码中或配置文件中指定你打算用那个具体的日志系统。只需要在项目中加入定的slf4j-logo.jar包就可以。这个和Spring的IOC思想有点 像,想用哪个就用哪个,随意切换。

使用slf4j这种记录日志的方式的特点很明显:

1、如果我们需要记录的是 底层类库或者组件这种东西,就可以不影响或强制要求用户 选择哪种日志系统。

2、如果我们用特定的日志系统写了代码,但是有一天要求换另一种日志系统,如果之前就用slf4j的api写的,那就很简单,如果用具体的一种方式写的,那切换就很麻烦。


具体使用哪种日志系统,这个可以随意切换,演示如下:

1、使用slf4j的simple日志系统:

在classpath中加入:slf4j-simple.jar,运行程序输入如下:

[main] ERROR com.swu.gusi.Slf4jTest - [info message]

[main] INFO com.swu.gusi.Slf4jTest - [info message]abc,false,123,com.swu.gusi.Slf4jTest@503f0b70

hello world

2、切换成jdk的日志系统:

在classpath中将 slf4j-logo.jar 换成slf4j-jdk.jar,运行程序如下:

九月 20, 2014 12:33:52 下午 com.swu.gusi.Slf4jTest main

严重: [info message]

九月 20, 2014 12:33:52 下午 com.swu.gusi.Slf4jTest main

信息: [info message]abc,false,123,com.swu.gusi.Slf4jTest@400da341

hello world

3、切换成log4j的日志系统:

在classpath中将slf4j-logo.jar换成slf4j-log4j.jar,注意这在使用log4j的时候,需要自己配置log4j.properites文件或者log4j.xml。至于log4j的使用查阅其他文档。

4、切换成其他的日志系统和上面相同。

5、如果classpath中有多种日志系统,会怎么样呢。会提示类似如下信息(最好别这样搞):

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/E:/Repository/org/slf4j/slf4j-simple/1.7.7/slf4j-simple-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/E:/Repository/org/slf4j/slf4j-jdk14/1.7.7/slf4j-jdk14-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]

[main] ERROR com.swu.gusi.Slf4jTest - [info message]

[main] INFO com.swu.gusi.Slf4jTest - [info message]abc,false,123,com.swu.gusi.Slf4jTest@10e80317

hello world

公司规范

http://wiki.corp.xxxxxx.com/pages/viewpage.action?pageId=63243043

slf4j(simple logging facade for java)的更多相关文章

  1. SLF4J (The Simple Logging Facade for Java)使用记录

    SLF4J (The Simple Logging Facade for Java)使用记录 官网 http://www.slf4j.org/ 参考资料 官方文档 什么是 SLF4J? 官网: The ...

  2. Simple Logging Facade for Java 简单日志门面(Facade)

    SLF4J是为各种 loging APIs提供一个简单统一的接口,从而使得最终用户能够在部署的时候配置自己希望的loging APIs实现.Logging API实现既可以选择直接实现SLF4J接口的 ...

  3. 为什么使用SLF4J比使用log4j或者java.util.logging更好

    1.SLF4j是什么? SLF4J 并没有真正地实现日志记录,它只是一个允许你使用任何java日志记录库的抽象适配层. 如果你正在编写内部或者外部使用的API或者应用库的话,如果使用了slf4j,那么 ...

  4. sprinbcloud学习之-Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>

    日志报错,提示Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>, 原因为 ...

  5. slf4j/logback: logging日志的配置

    slf4j/logback: logging日志的配置 import依赖: import org.slf4j.Logger;import org.slf4j.LoggerFactory;private ...

  6. Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String, java.lang.String>

    org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'log ...

  7. SLF4j:Log facade abstract

    内 容: 应用中使用slf4j的工作流程 简单示例 ILoggerFactory实例化过程 由ILoggerFactory创建Logger实例 slf4j 适配器实现 自定义适配器 slf4j 如何选 ...

  8. How to enable wire logging for a java HttpURLConnection traffic?

    https://stackoverflow.com/questions/1445919/how-to-enable-wire-logging-for-a-java-httpurlconnection- ...

  9. java日志组件介绍(common-logging,log4j,slf4j,logback )

    转自:http://www.blogjava.net/daiyongzhi/archive/2014/04/13/412364.html common-logging是apache提供的一个通用的日志 ...

随机推荐

  1. Struts国际化

    第一步需要建立配置文件 格式为      文件名_zh_CN.properties    为中文配置文件   文件名_en_US.properties为美式英语配置文件 配置文件里面的值以键值对的形式 ...

  2. LeetCode OJ 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  3. blur事件

    blur事件是在元素失去焦点的时候触发,那么失去焦点的前提便是获得焦点. 哪些元素可以获取焦点呢? 1.超链接 2.input button textarea (without disabled) 3 ...

  4. 基于 twemproxy 搭建 redis 集群

    概述 由于单台redis服务器的内存管理能力有限,使用过大内存redis服务器的性能急剧下降,且服务器发生故障将直接影响大面积业务.为了获取更好的缓存性能及扩展型,我们将需要搭建redis集群来满足需 ...

  5. MVC DisplayTemplates and EdiotrTemplates.

    我们在mvc项目里经常要对枚举,日期,副文本输入,我们可以用笨拙的方法去view页面里绑定呈现的html内容,而且这种办法不能重用,也就是在不同的view里还是需要做相同的事情,给个日期空间选择例子吧 ...

  6. Flask -- 路由

    route()装饰器把一个函数绑定到对应的URL(可以是多个)上 @app.route('/') def index(): return 'Index Page' @app.route('/hello ...

  7. VBS脚本操作网页元素

    =================打开百度,点击百度按钮==================== Dim btn,ieSet ie = WScript.CreateObject("Inter ...

  8. MFC单选按钮

    先为对话框加上2个radio button,分别是Radio1和Radio2. 问题1:如何让Radio1或者Radio2默认选上?如何知道哪个被选上了? 关键是选上,“默认”只要放在OnInitDi ...

  9. OpenLDAP安装与配置

    系统:ubuntu 14.04 安装: 1. sudo apt-get install slapd ldap-utils 2. 在1的过程中会让你输了admin密码 配置: 如果安装过,只是想配置Op ...

  10. how to use the curses library in unix?

    In linux, you can use the ncurses library to use the terminal as a text buffer: move the cursor arou ...