SLF4J: No SLF4J providers were found.完美解决

背景

在项目中引入log4j时,首先考虑项目中所用到的框架的日志本身对log4j的兼容性,为此需要引入一个各种日志的抽象层slf4j,该抽象层使得你无论在项目中用什么日志框架,都可以很好的与项目中其他框架的日志兼容。所以,如果我们要使用log4j作为项目的日志框架,那么为了和项目中其他的框架的日志兼容,我们需要引入slf4j-api以及slf4j-log4j12。

slf4j的引入

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.0-alpha1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>2.0.0-alpha1</version>
            <scope>test</scope>
        </dependency>

报错解决

此时启动项目,开头就报错

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.

根据提示阅读文档 http://www.slf4j.org/codes.html#noProviders 可知,slf4j在1.8.0之后的机制不一样了,所以我们换一个1.8.0之前的版本试一下
在这里插入图片描述
slf4j-api换成1.7.30后重新启动项目

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

可以看到,刚才的错误解决了,又遇到一个新的错误,继续根据给的文档进行查找问题 http://www.slf4j.org/codes.html#StaticLoggerBinder
在这里插入图片描述
可以看到,无论是第一个错误还是第二个错误,都是依赖版本的问题,官方在Jdk1.9开始对于slf4j-api以及其他如slf4j-log4j12等依赖的绑定机制不同了,Jdk1.9对应slf4j-api以及slf4j-log4j12的版本从1.8.0开始。所以,为了解决以上两个问题,(如果我们使用的是Jdk1.8及以下)我们可以直接降低这两个依赖的版本到1.8.0以下,或者升级为Jdk1.9再使用这两个依赖的版本为1.8.0以上。

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.30</version>
<!--            <scope>test</scope>-->
        </dependency>

!!!特别注意:以上slf4j-log4j12的scope要么直接去掉,要么改为compile,因为test代表只有在测试(@Test)的时候该依赖才起作用。

  • 18
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那个小白猿

讨杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值