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

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


当前回答

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

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

SLf4J站点中有一个文档可以解决这个问题。随后,我在应用程序中添加了slf4j-simple-1.6.1.jar以及我已经拥有的slf4j-api-1.6.1.jar。

如果您使用Maven,请使用${slf4j添加以下依赖项。Version}是slf4j的最新版本

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

这解决了我的问题。

其他回答

作为jar包含和纯maven解决方案的替代方案,您可以使用gradle从maven中包含它。

版本为1.7.25的示例

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

把它放在构建的依赖项中。gradle文件。

我知道这篇文章有点老了,但以防其他人遇到这个问题:

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

HTH 彼得

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

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包

您的问题很可能是因为<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>

对我来说,总的解决方案是:

1

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

plus

2创建文件log4j.properties

然后在里面加上:

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

否则我在控制台有一些异常。