SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.

错误日志如下:

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.

项目使用的日志是Log4j2 和SLF4J,依赖如下:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.13.3</version>
        </dependency>
          <!-- slf4j核心包-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.13.3</version>
            <scope>test</scope>
        </dependency>

眼尖的同学可能已经看出问题所在了,不过当时我没有完全看出来。明明已经在pom.xml中添加了log4j-slf4j-impl的依赖了,为什么slf4j会提示找不到绑定的日志。
然后,我去target/webapp/lib文件专门看一下发现,确实不存在 log4j-slf4j-impl的jar包。想着会不会是maven出问题了。执行maven的clean命令,再重新部署,发现并没有解决问题。
然后,考虑是不是版本比较新,改了版本号还是没法解决。
最后,我突然发现了log4j-slf4j-impl的jar包test。把这个去掉,问题就解决了。


程序可以正常运转了,不过问题并没有彻底解决。test并不是我自己添加的,整个 元素都是从Maven中央仓库复制过来的。为什么官方会给这个依赖添加上test?

我在SLF4J官网的问答上截取了这么一段话:

When should SLF4J be used?
In short, libraries and other embedded components should consider SLF4J for their logging needs because libraries cannot afford to impose their choice of logging framework on the end-user. On the other hand, it does not necessarily make sense for stand-alone applications to use SLF4J. Stand-alone applications can invoke the logging framework of their choice directly. .

谷歌翻译如下:

简而言之,库和其他嵌入式组件应考虑SLF4J的日志记录需求,因为库无法负担将其日志记录框架的选择强加于最终用户。另一方面,独立应用程序使用SLF4J不一定有意义。独立应用程序可以直接调用其选择的日志记录框架。

根据上面这段话可以看出来,SLF4J主要是用在库和嵌入式组件中。而这些组件并不应该也不需要配置具体的日志框架。SLF4J之所以会提供log4j-slf4j-impl之类的jar包,主要是为了测试使用。

你可能感兴趣的:(bug处理,idea,框架)