Slf4j、log4j、logback介绍

简介

下图来源于slf4j官网,从图中来看,可以得到如下信息:

  1. slf4j是接口;
  2. log4j、logback、java.util.logging、slf4j-simple、slf4j-nop是slf4j接口的一种的实现;
  3. 从图片的第三行观察可以发现(通过图表的颜色进行区分):
    1. logback、slf4j-simple、slf4j-nop直接原生实现了slf4j的接口;
    2. log4j、java.util.logging没有直接实现slf4j接口,所以需要的适配类将slf4j接口和具体实现进行绑定。
  4. 如果要开发一个libraray或者SDK供别人使用,最好的方式是只在dependency中添加slf4j-api.jar包,这样最后使用哪个日志就是用户自己来决定的。用户只需在自己的程序的classpath中提供不同的Jar包即可采用不同的日志实现方法。

用法

由上图我们也可进一步得到如下信息:

日志实现 用户需提供的包
log4j slf4j-reload4-xx.jar(适配器)和reload4-xx.jar(实现)
logback logback-classic-xx.jar(该包默认包含logback-core-xx.jar)
java.util.logging slf4j-jdk14.jar(适配器) (jvm runtime,默认在jvm中已经存在)
slf4j-simple slf4j-simple-xx.jar
slf4j-nop slf4j-nop-xx.jar

注意:

  1. 上表中的xx最好和slf4j-api的版本一致。
  2. 如果用户是引用了基于Slf4j接口的SDK,则需要提供上面的Jar包,如果用户是自己写日志,则需同时提供slf4j-api-xx.jar

兼容性

  1. slf4j-api之间是兼容的,即slf4j-Nslf4j-M对于任意的N和M都是兼容的。
  2. 唯一需要确保的是bindingslf4j-api兼容,例如,如果使用slf4j-api-N.jar,则必须使用slf4j-simple-N.jar,或者slf4j-log4j12-N.jar。在初始化的时候,如果发现slf4jbinding不匹配,则slf4j会抛出一条警告消息。
  3. 如果已经在项目中显示的声明了slf4j-apislf4j-log4j,则无需关心你依赖的别的包,间接再依赖别的slf4j-api版本,因为根据Maven仲裁原理,maven查找时,只会用你项目中声明的版本。

原理

以下内容引用自官网:

SLF4J does not rely on any special class loader machinery. In fact, each SLF4J binding is hardwired at compile time to use one and only one specific logging framework. For example, the slf4j-log4j12-1.7.36.jar binding is bound at compile time to use log4j. In your code, in addition to slf4j-api-1.7.36.jar, you simply drop one and only one binding of your choice onto the appropriate class path location. Do not place more than one binding on your class path.

As of SLF4J version 1.6.0, if no binding is found on the class path, then slf4j-api will default to a no-operation implementation discarding all log requests. Thus, instead of throwing a NoClassDefFoundError because the org.slf4j.impl.StaticLoggerBinder class is missing, SLF4J version 1.6.0 and later will emit a single warning message about the absence of a binding and proceed to discard all log requests without further protest. For example, let Wombat be some biology-related framework depending on SLF4J for logging. In order to avoid imposing a logging framework on the end-user, Wombat’s distribution includes slf4j-api.jar but no binding. Even in the absence of any SLF4J binding on the class path, Wombat’s distribution will still work out-of-the-box, and without requiring the end-user to download a binding from SLF4J’s web-site. Only when the end-user decides to enable logging will she need to install the SLF4J binding corresponding to the logging framework chosen by her.

大概意思就是:

  1. slf4j只是接口,用户可以在classpath提供不同的bingding Jar包,从而采用不同的日志实现方式;
  2. 如果没找到bingding jar的实现类,则不输出日志。

Slf4j类查找和加载机制

slf4j-api now relies on the ServiceLoader mechanism to find its logging backend. SLF4J 1.7.x and earlier versions relied on the static binder mechanism which is no loger honored by slf4j-api version 2.0.x. More specifically, when initializing the LoggerFactory class will no longer search for the StaticLoggerBinder class on the class path.

Instead of “bindings” now org.slf4j.LoggerFactory searches for “providers”. These ship for example with slf4j-nop-2.0.x.jar, slf4j-simple-2.0.x.jar or slf4j-jdk14-2.0.x.jar.

附:使用方法示例

logback

使用方法,添加如下依赖即可。注:该依赖会将logback-coreslf4j-api也引入项目,但是如果想要显示的声明slf4j-api和logback-core的版本,则需要在dependency中添加指定版本的依赖即可。

根据Maven的nearest definition原则,会优先使用自己声明的版本。

<dependency> <groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.10</version>
</dependency>

Reload4j

<dependency> <groupId>org.slf4j</groupId><artifactId>slf4j-reload4j</artifactId><version>1.7.36</version>
</dependency>

Log4j

<dependency> <groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.36</version>
</dependency>
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.7.36</version><scope>provided</scope><exclusions><exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion></exclusions>
</dependency>

Slf4j、log4j、logback介绍相关推荐

  1. slf4j log4j logback关系详解和相关用法 【by Sinte-Beuve】

    slf4j log4j logback关系详解和相关用法 slf4j log4j logback的关系 The Simple Logging Facade for Java是什么? log4j和log ...

  2. SpringMVC学习(三)——SpringMVC+Slf4j+Log4j+Logback日志集成实战分享

    文章目录 1.概述 1.1 说明 1.2 日志体系 1.2.1 JCL日志面门介绍 1.2.2 Slf4j日志面门介绍 2.几种日志系统介绍: 2.1 Slf4j 2.2 Commons-loggin ...

  3. java log4j和logback,跨过slf4j和logback,直接晋级log4j 2

    今年一直关注log4j 2,但至今还没有出正式版.等不及了,今天正式向大家介绍一下log4j的升级框架,log4j 2. log4j,相信大家都熟悉,至今对java影响最大的logging系统,至今仍 ...

  4. Java各类日志门面(slf4j,commons-logging)和日志框架(log4j,logback)联系和区别

    日志门面 1.Apache通用日志接口(commons-logging.jar) Apache Commons包中的一个,包含了日志功能,必须使用的jar包.这个包本身包含了一个Simple Logg ...

  5. log4j+logback+slf4j+commons-logging的关系与调试

    2019独角兽企业重金招聘Python工程师标准>>> 背景      由于现在开源框架日益丰富,好多开源框架使用的日志组件不尽相同.存在着在一个项目中,不同的版本,不同的框架共存. ...

  6. 【Java从0到架构师】日志处理 - SLF4J、Logback、Log4j 2.x

    日志处理 - SLF4J.Logback.Log4j 2.x SLF4J + Log4j 1.x SLF4J + Logback Logback - 配置文件 Logback - 控制台彩色打印 Lo ...

  7. SLF4J 之logback.xml配置文件实例及其说明

    为什么80%的码农都做不了架构师?>>>    对于java后端程序员来说,如何记录日志是一个小话题,只要在网上随便找一个log4j的简单说明,就可以用了.但是,要真正了解日志记录的 ...

  8. 日志管理(一):slf4j原理简单介绍

    转载自:http://blog.sina.com.cn/s/blog_6f67b91d0100tpqh.html 全称:Simple Logging Facade for Java  简单日志门面(F ...

  9. java log4j logback jcl_Java 日志二三事

    前言 Java 拥有功能和性能都非常强大的日志库,但另一方面,Java 日志库依赖看起来丰富的让人眼花缭乱.相信大家或多或少都有这样的疑问,Log4j,SLF4J,Logback,Log4j2 这些日 ...

最新文章

  1. Java 统计字母个数
  2. redis常用配置项
  3. [转]T4模版引擎之生成数据库实体类
  4. 【STM32】USART相关函数和类型
  5. WebSocket服务器TransactionID_SiteDetailMap的析构工作
  6. matlab求kcf算法响应图_Kernelized Correlation Filters(KCF)算法
  7. php判断电脑浏览器模拟手机访问网页,在PC上测试移动端网站和模拟手机浏览器的5大方法...
  8. 发明喂饭机器人_人类又懒出新高度,老美发明自动喂饭机器人,“君子”动嘴不动手...
  9. vnpy学习10_常见坑
  10. 高德开放平台与360儿童手表达成合作,全球数据助力第三方企业
  11. MySQL的外键约束:Cascade/Restrict/No action/SET NULL :级联操作
  12. 什么能让我们用青春、用生命去垫付?
  13. emi滤波matlab,EMI电源滤波器选型方法 浅析EMI电源滤波器及其原理介绍
  14. iphone8进入恢复模式或DFU模式
  15. 支持向量机SVM--线性
  16. python爬取问卷星内容,Python 问卷星自动填写 爬虫
  17. 配置路由协议rip和ospf
  18. java字符串长度(java字符串长度压缩)
  19. php绕过授权下载,Discuz附件下载权限绕过方法
  20. python情人节之玫瑰花与表白方式

热门文章

  1. python request使用
  2. 服务器临时维护 开启另行通知,倩女幽魂手游6月23日在线维护公告内容介绍
  3. 宝玉的blog 专注于web开发技术
  4. BU客户端发布新版本,删除了BSV协议功能
  5. c语言选择排序详解及代码,C语言选择排序算法及实例代码
  6. 关于C语言数据基本类型的Signed和Unsigned的理解
  7. dwcss样式中英对照_dw-cs5-css规则英汉对照表.docx
  8. java ioctl,ioctl函数详细说明
  9. 矿山井下IT配电系统的应用
  10. 北航计算机学院院长刘旭东,做点燃学生内心火焰的点灯人——记北京航空航天大学立德树人卓越奖获得者刘旭东教授...