首页 文章

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

提问于
浏览
420

我的应用程序将部署在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 .

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

24 回答

  • 13

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

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

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

    例如,将上述代码添加到 build.gradle 或maven项目的相应代码 pom.xml .

  • 0

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

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

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

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

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

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

  • 2

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

  • 7

    在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.
    

    当我在 libs 中使用 slf4j-api-1.7.5.jar 时 .

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

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

  • 37

    请将以下依赖项添加到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>
    
  • 4

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

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

    Failed to load class "org.slf4j.impl.StaticLoggerBinder"

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

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

  • 13

    使用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

    作为jar包含和纯maven解决方案的替代方案,您可以使用gradle包含它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 文件的依赖项中 .

  • 2

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

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

  • 15

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

  • 0

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

    https://mvnrepository.com/artifact/org.slf4j/slf4j-simple,您将找到最新版本的SLF4J Simple Binding . 选择最适合你的那个(2017-03至2017年的1.7.25是2018-02的稳定版本)并将其包含在你的pom.xml中 .

    Beta Version of March 2018

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

    Stable Version

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

    根据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一起添加到类路径中即可完成任务 . 祝你好运

  • 10

    只需将其添加到 pom.xml

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

    正如SLF4J Manual所述

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

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

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

    NoOp binding (slf4j-nop)

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

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

    Simple binding (slf4j-simple)

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

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

    Bindings for the logging frameworks (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

  • 1

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

    如果您使用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是底层日志框架的外观,如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

  • 4

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

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

    HTH彼得

  • 2

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

  • 5

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

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

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

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

  • 23

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

    无法加载类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) Logger 实现 . 如果您负责打包应用程序而不关心日志记录,那么将slf4j-nop.jar放在应用程序的类路径上将消除此警告消息 . 请注意,嵌入式组件(如库或框架)不应声明对任何SLF4J绑定的依赖性,而仅依赖于slf4j-api . 当库声明对SLF4J绑定的编译时依赖性时,它会对最终用户强制绑定,从而否定SLF4J的用途 .

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

  • 0

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

  • 1

    我使用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

相关问题