SLF4J加载类org.slf4j.impl.StaticLoggerBinder失败

一、Console控制台输出异常,加载类 org.slf4j.impl.StaticLoggerBinder失败

IDEA 运行时,Console控制台输出信息:

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.


SLF4J加载类org.slf4j.impl.StaticLoggerBinder失败


上面的意思是:加载类文件org.slf4j.impl.StaticLoggerBinder时失败。在缺少binding的情况下,默认加载NOP logger实现。


二、官网分析和说明

可以访问上面提示的链接,查看官网提供的解决方案:

This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

SINCE 1.6.0 As of SLF4J version 1.6, in the absence of a binding, SLF4J will default to a no-operation (NOP) logger implementation.


中文说明:

无法加载类 org.slf4j.impl.StaticLoggerBinder

当无法将类org.slf4j.impl.StaticLoggerBinder加载到内存中时,会报告此警告消息 。如果在类路径上找不到合适的SLF4J绑定,就会发生这种情况。在类路径中放置slf4j-nop.jar、slf4j-simple.jar、slf4j-log4j12.jar、slf4j-jdk14.jar或 logback-classic.jar中的一个(并且只有一个)应该可以解决问题。

自1.6.0起,SLF4J版本1.6 以来,在没有绑定的情况下,SLF4J将默认为无操作(NOP)记录器实现。


如果您负责打包应用程序并且不关心日志记录,那么将slf4j-nop.jar放在应用程序的类路径中将摆脱此警告消息。请注意,嵌入式组件(如库或框架)不应声明对任何SLF4J绑定的依赖关系,但仅依赖于slf4j-api。当库声明对SLF4J绑定的编译时(compile-time)依赖性时,它会将该绑定强加给最终用户,从而否定SLF4J的目的。


三、具体解决方案

解决的办法就是在Maven工程的pom文件中随便加载一个上述的包文件之一的依赖就可以了(且只有一个),此时我加载的是“slf4j-nop-1.7.25.jar”包文件的依赖,然后整个项目就可以编译无warning警告了。

<dependency>

<groupid>org.slf4j/<groupid>

<artifactid>slf4j-nop/<artifactid>

<version>1.7.25/<version>


或者添加slf4j-log4j12.jar包依赖:

<dependency>

<groupid>org.slf4j/<groupid>

<artifactid>slf4j-log4j12/<artifactid>

<version>1.7.25/<version>


添加好依赖包之后,再次编译就没有前面的警告信息了。

SLF4J加载类org.slf4j.impl.StaticLoggerBinder失败


分享到:


相關文章: