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

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


当前回答

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

其他回答

我也遇到过类似的问题,通过以下方法解决了问题:

文件->项目结构… 模块->依赖项 从Maven ->搜索:slf4j.nop 单击最新版本-> Apply

在payara 5.191上遇到了同样的问题

Jcl-over-slf4j和slf4j-log4j12一起解决了这个问题

<properties>
  <slf4j.version>1.7.29</slf4j.version>
</properties>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>${slf4j.version}</version>
  <type>jar</type>
</dependency> 

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>${slf4j.version}</version>
</dependency>        

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>${slf4j.version}</version>
</dependency>

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

您的问题很可能是因为<scope>test</scope>(在某些情况下也<scope>提供</scope>),正如前面提到的@thangaraj。

文档中说:

此范围表示正常情况下不需要依赖项 应用程序的使用,并且仅可用于测试编译 以及执行阶段。测试依赖关系是不可传递的,并且只存在于测试和执行类路径中。

所以,如果你不需要测试依赖项,那么你可以使用代替(你将在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>

这相当于:

 <!-- 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>

在将logback-classic升级到最新版本:1.4.5之后,我遇到了这个问题SLF4J:未能加载类“org.slf4j.impl”。StaticLoggerBinder”。logback-classic: 1.4。X没有org.slf4j.impl类。StaticLoggerBinder,这就是问题所在。

供您参考:slf4j-api 2.0。x将不再搜索StaticLoggerBinding。详情请参阅https://www.slf4j.org/faq.html#changesInVersion200。