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

Slf4j、log4j、logback介绍_第1张图片

用法

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

日志实现 用户需提供的包
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原则,会优先使用自己声明的版本。

 
  ch.qos.logback
  logback-classic
  1.2.10

在这里插入图片描述

Reload4j

 
  org.slf4j
  slf4j-reload4j
  1.7.36

Log4j

 
  org.slf4j
  slf4j-log4j12
  1.7.36


    log4j
    log4j
    1.7.36
    provided
    
        
            *
            *
        
    

你可能感兴趣的:(学习笔记,maven,slf4j,log4j,logback)