Slf4j、log4j、logback介绍

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包即可采用不同的日志实现方法。

在这里插入图片描述

用法

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

日志实现用户需提供的包
log4jslf4j-reload4-xx.jar(适配器)和reload4-xx.jar(实现)
logbacklogback-classic-xx.jar(该包默认包含logback-core-xx.jar)
java.util.loggingslf4j-jdk14.jar(适配器) (jvm runtime,默认在jvm中已经存在)
slf4j-simpleslf4j-simple-xx.jar
slf4j-nopslf4j-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>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SLF4Jlog4jlogback是Java的三个不同的日志框架。SLF4J是Java的一个日志门面,它提供了一些通用的API,可以与不同的具体日志框架集成使用。log4j是一个具体的日志框架,它提供了丰富的功能和配置选项。logback则是由log4j的作者设计完成的一个日志框架,它拥有更好的特性,并且是SLF4J的原生实现。 区别如下: 1. SLF4J是一个日志门面,它只提供了一些通用的API,而不是具体的实现。它的作用是为了让开发人员可以在不同的日志框架之间进行切换和集成,而不需要修改代码。 2. log4j是一个具体的日志框架,它提供了丰富的功能和配置选项。log4j可以与SLF4J结合使用,需要提供一些对应的jar包。 3. logback是由log4j的作者设计完成的日志框架,它是SLF4J的原生实现。logback拥有更好的特性,并且可以完整地实现SLF4JAPIlogback包括了三个模块:logback-core、logback-classic和logback-access,分别用于提供基础功能、改良版本以及与Servlet容器集成。 因此,SLF4J提供了通用的日志接口,log4j是其中一个具体的实现,而logback则是log4j的改良版本,同时也是SLF4J的原生实现。根据具体需求和偏好,开发人员可以选择使用其中的任意一个日志框架。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java日志框架SLF4Jlog4j以及logback的联系和区别](https://blog.csdn.net/weixin_30241919/article/details/101487496)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值