SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”

问题描述 投票:489回答:28

我的应用程序将部署在tcServer和WebSphere 6.1上。此应用程序使用ehCache,因此需要slf4j作为依赖项。结果我将slf4j-api.jar(1.6)jar添加到我的war文件包中。

该应用程序在tcServer中正常工作,但以下错误除外:

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.

但是,当我在WebSphere中部署时,我得到了一个java.lang.NoClassDefFoundError: org.slf4j.impl.StaticLoggerBinder

我检查了两个应用程序服务器的类路径,没有其他的slf4j jar。

有没有人有任何想法可能会发生在这里?

java websphere slf4j
28个回答
447
投票

我在WebSphere 6.1中遇到了同样的问题。正如Ceki指出的那样,WebSphere正在使用大量的罐子,其中一个指向旧版本的slf4j。

No-Op回滚只发生在slf4j -1.6+上,因此任何比这更旧的东西都会抛出异常并停止部署。

SLf4J site中有一个文档可以解决这个问题。我跟着它,并将slf4j-simple-1.6.1.jar与我已经拥有的slf4j-api-1.6.1.jar一起添加到我的应用程序中。

这解决了我的问题。希望它可以帮助那些有这个问题的人。


8
投票

SLF4j是各种日志框架的抽象。因此,除了拥有slf4j之外,您还需要在类路径中包含任何日志框架,如log4j或logback(etc)。 有想法参考http://logback.qos.ch/manual/introduction.html的第一个婴儿步骤


5
投票

Slf4j是底层日志框架的外观,如log4j,logback,java.util.logging。

为了连接底层框架,slf4j使用绑定。

  • log4j - slf4j-log4j12-1.7.21.jar
  • java.util.logging - slf4j-jdk14-1.7.21.jar等

如果错过了绑定jar,则抛出上述错误。您可以下载此jar并将其添加到classpath。

对于maven依赖,

<dependency> 
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.21</version>
</dependency>

除了slf4j-log4j12-1.7.21.jar之外,这个依赖项还会将slf4j-api-1.7.21.jar以及log4j-1.2.17.jar引入到你的项目中

参考:http://www.slf4j.org/manual.html


5
投票

我遇到了与Java 9库的Spring-boot-2应用程序类似的问题。

在我的pom.xml中添加以下依赖项为我解决了这个问题:

    <dependency>
        <groupId>com.googlecode.slf4j-maven-plugin-log</groupId>
        <artifactId>slf4j-maven-plugin-log</artifactId>
        <version>1.0.0</version>
    </dependency>

4
投票

在Websphere案例中,您有一个旧版本的slf4j-api.jar,1.4.x。或1.5.x位于某处。您在tcServer上观察到的行为,即故障转移到NOP,发生在slf4j版本1.6.0及更高版本上。确保在所有平台上使用slf4j-api-1.6.x.jar,并且没有在类路径上放置旧版本的slf4j-api。


4
投票

当我收到以下错误时,我遇到了这个问题:

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-api-1.7.5.jar中使用libs时。

尽管我尝试了整个建议的补充罐子,如slf4j-log4j12-1.7.5.jarslf4j-simple-1.7.5,但错误信息仍然存在。当我将slf4j-jdk14-1.7.5.jar添加到java libs时,问题终于得到了解决。

http://www.slf4j.org/download.html获取整个slf4j包


4
投票

请将以下依赖项添加到pom以解决此问题。

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.25</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
</dependency>

3
投票

我在Struts2 + Spring项目中工作。所以它需要依赖slf4j-api-1.7.5.jar

如果我运行该项目,我会收到错误

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

我通过添加slf4j-log4j12-1.7.5.jar解决了我的问题。

因此,在项目中添加此jar以解决问题。


3
投票

正如SLF4J Manual所述

Simple Logging Facade for Java(SLF4J)可用作各种日志框架的简单外观或抽象,例如java.util.logging,logback和log4j。

只要在类路径中添加绑定,警告就会消失。

因此,您应该选择要使用的绑定。

NoOp绑定(slf4j-nop)

绑定NOP,静默丢弃所有日志记录。

查看https://search.maven.org/search?q=g:org.slf4j%20AND%20a:slf4j-nop&core=gav的新版本

简单绑定(slf4j-simple)

将所有事件输出到System.err。仅打印I​​NFO级别以上的消息。此绑定在小应用程序的上下文中可能很有用。

查看https://search.maven.org/search?q=g:org.slf4j%20AND%20a:slf4j-simple&core=gav的新版本

日志框架的绑定(java.util.logging,logback,log4j)

如果要将日志写入文件,则需要其中一个绑定。

请参阅https://www.slf4j.org/manual.html#projectDep上的说明和说明


我的看法

我建议使用Logback,因为它是log4j项目的后续版本。

https://search.maven.org/search?q=g:ch.qos.logback%20AND%20a:logback-classic&core=gav上查看最新版本的绑定

您可以获得开箱即用的控制台输出,但如果您需要将日志写入文件,只需将FileAppender配置放入src/main/resources/logback.xmlsrc/test/resources/logback-test.xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>logs/logs.log</file>

        <encoder>
            <pattern>%date %level [%thread] %logger{10} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

    <logger level="DEBUG" name="com.myapp"/>
</configuration>

(参见手册中的详细说明:https://logback.qos.ch/manual/configuration.html


2
投票

我添加了此依赖项来解决此问题:

https://mvnrepository.com/artifact/org.slf4j/slf4j-simple/1.7.25

2
投票

作为jar包含和纯maven解决方案的替代方案,您可以将它包含在from maven中。

版本1.7.25的示例

// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
api group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'

把它放在你的build.gradle文件的依赖项中。


303
投票

这是为那些从谷歌搜索来到这里的人。

如果您使用maven,只需添加以下内容即可

   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.5</version>
   </dependency>
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.5</version>
   </dependency>

要么

   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.5</version>
   </dependency>
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-simple</artifactId>
       <version>1.6.4</version>
   </dependency>

1
投票

根据SLF4J官方文件

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

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

只需将this jar和slf4j api.jar一起添加到类路径中即可完成任务。祝你好运


1
投票

我解决了它添加这个库:slf4j-simple-1.7.25.jar你可以在官方网站https://www.slf4j.org/download.html下载


1
投票
     <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>

将上述依赖项放在pom.xml文件中


0
投票

我知道这篇文章有点旧,但万一有其他人遇到这个问题:

将slf4j-jdk14-X.X.X.jar添加到您的CLASSPATH(其中X.X.X是版本号 - 例如slf4j-jdk14-1.7.5.jar)。

HTH彼得


0
投票

我使用Jena并将以下依赖项添加到pom.xml

<dependency> 
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.0.13</version>
</dependency>

我尝试添加slf4j-simple但它只是消失了“SLF4J:无法加载类”org.slf4j.impl.StaticLoggerBinder“”错误但logback-classic显示更多详细信息。

official document


0
投票

解决方案在他们的官方网站上显示:

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

无法将org.slf4j.impl.StaticLoggerBinder类加载到内存中时会报告此警告消息。如果在类路径上找不到合适的SLF4J绑定,则会发生这种情况。在类路径上放置一个(并且只有一个)slf4j-nop.jar slf4j-simple.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或logback-classic.jar应该可以解决问题。 SINCE 1.6.0从SLF4J版本1.6开始,在没有绑定的情况下,SLF4J将默认为无操作(NOP)记录器实现。如果您负责打包应用程序而不关心日志记录,那么将slf4j-nop.jar放在应用程序的类路径上将消除此警告消息。请注意,嵌入式组件(如库或框架)不应声明对任何SLF4J绑定的依赖性,而只依赖于slf4j-api。当库声明对SLF4J绑定的编译时依赖性时,它会对最终用户强制绑定,从而否定SLF4J的用途。

解决方案:我已经在intellij上使用maven研究添加到我的项目中,我选择了slf4j-jdk14.jar。


0
投票

很可能你的问题是因为<scope>test</scope>(在某些情况下还有<scope>provided</scope>),如@thangaraj所提到的那样。

Documentation说:

此范围表示正常使用应用程序不需要依赖关系,并且仅可用于测试编译和执行阶段。 Test dependencies aren’t transitive and are only present for test and execution classpaths.

因此,如果您不需要依赖项用于测试目的,那么您可以使用而不是(您将在qazxsw poi中看到的内容):

mvnrepository

没有任何范围(默认情况下,当没有提供其他范围时,它将是编译范围):

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-nop</artifactId>
    <version>1.7.24</version>
    <scope>test</scope>
</dependency>

这与:

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>  
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-nop</artifactId>
   <version>1.7.25</version>
</dependency>

0
投票

这是我的5美分......

我在运行测试时遇到了同样的问题。所以我通过仅为测试运行时添加实现来修复它。我正在使用gradle进行这个项目。

// <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-nop</artifactId> <version>1.7.25</version> <scope>compile</scope> </dependency>

testRuntimeOnly group:'ch.qos.logback',name:'logback-classic',version:'1.2.3'


0
投票

对我来说问题是:使用Hibernate,我看到它已经使用了slf4j,它已经在我的类路径中,所以我决定使用它。下一步 - 为slf4j添加imlementor,所以我添加到maven:

https://mvnrepository.com/artifact/ch.qos.logback/logback-classic

但它失败了,错误! SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”

解决方案是:Hibernate对slf4j的依赖是版本1.7.26,我添加了次要版本依赖1.7.25。所以当我解决这个问题时 - 一切都变好了


41
投票

您需要在类路径中添加以下jar文件:slf4j-simple-1.6.2.jar。如果您没有,请下载。请参考http://www.slf4j.org/codes.html#multiple_bindings


34
投票

只需将其添加到您的pom.xml:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.7.21</version>
</dependency>

26
投票

这里有一些答案建议将slf4j-simple依赖项添加到你的maven pom文件中。您可能想要检查最新版本。

https://mvnrepository.com/artifact/org.slf4j/slf4j-simple你会找到最新版本的SLF4J Simple Binding。选择最适合你的那个(从2019-02开始1.7.26仍是2019-07的稳定版本)并将其包含在你的pom.xml中。

为方便起见,这里显示了一些依赖项 - 但是当您阅读本文时它们可能不是最新的!

2019-06的Alpha版本

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>2.0.0-alpha0</version>
</dependency>

Beta版2019年2月

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.8.0-beta4</version>
</dependency>

稳定版本2019-02

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version>1.7.26</version>
</dependency>

由于下面的评论,我删除了范围测试部分。


22
投票

我面临同样的错误。我在本地开发中配置了slf4j-api,slf4j-log4j12和log4j。所有配置都很好,但我从mvnrepository复制的slf4j-log4j12依赖项有测试范围<scope>test</scope>。当我删除它时,每件事都很好。

有时愚蠢的错误打破了我们的脑袋;)


18
投票

有时我们应该看到警告中的注释,例如。 SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

您可以搜索此警告来的原因。 将*slf4j-nop.jarslf4j-simple.jarslf4j-log4j12.jarslf4j-jdk14.jarlogback-classic.jar*中的一个罐子添加到类路径中应该可以解决问题。

compile "org.slf4j:slf4j-api:1.6.1" compile "org.slf4j:slf4j-simple:1.6.1"

例如,将上面的代码添加到你的build.gradle或相应的代码到pom.xml for maven project。


14
投票

将文件slf4j-log4j12-1.6.4.jar放在类路径中就可以了。


10
投票

如果您正在使用maven进行依赖关系管理,那么您只需在pom.xml中添加以下依赖项即可

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.5.6</version>
</dependency>

对于非Maven用户只需下载库并将其放入项目类路径中。

在这里你可以看到详细信息:http://www.mkyong.com/wicket/java-lang-classnotfoundexception-org-slf4j-impl-staticloggerbinder/

© www.soinside.com 2019 - 2024. All rights reserved.