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

同时伴有加载类“org.slf4j.impl.StaticMDCBinder”失败

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

有人知道这里会发生什么吗?


当前回答

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

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

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

解决方案是: Hibernate的slf4j依赖项是版本1.7.26,我添加了小版本依赖项1.7.25。当我修复了这个,一切都好了

其他回答

当我得到以下错误时,我陷入了这个问题:

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。

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

在http://www.slf4j.org/download.html上获得整个slf4j包

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

正如SLF4J手册所述

Java的简单日志Facade (SLF4J)就是一个简单的Facade 或者对各种日志框架进行抽象,例如 java.util。日志、logback和log4j。

and

一旦向类路径添加绑定,警告就会消失。

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

NoOp绑定(slf4j-nop)

绑定NOP,无声地丢弃所有日志记录。

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

简单绑定(slf4j-simple)

输出所有事件到System.err。只打印INFO及以上级别的消息。这种绑定在小型应用程序上下文中可能很有用。

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

日志框架的绑定(java.util。日志,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.xml或src/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)

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

这里有相当多的答案建议向maven pom文件添加slf4j-simple依赖项。您可能需要检查最新的版本。

在https://mvnrepository.com/artifact/org.slf4j/slf4j-simple 您将找到SLF4J简单绑定的最新版本。选择一个最适合你的版本(2021-07年的1.7.32仍然是2021-10年的稳定版本),并将其包含到你的pom.xml中。

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

2021-08测试版

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

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>

稳定版2021-07

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

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