我的应用程序将部署在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。
有人知道这里会发生什么吗?
Slf4j是底层日志框架(如log4j、logback、java.util.logging)的门面。
为了连接底层框架,slf4j使用了绑定。
log4j - slf4j-log4j12-1.7.21.jar
java.util.logging – slf4j-jdk14-1.7.21.jar etc
如果没有绑定jar,就会抛出上述错误。您可以下载这个jar并将其添加到类路径中。
对于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
对我来说,问题是:
在使用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。当我修复了这个,一切都好了
这里有相当多的答案建议向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>
由于下面的评论,我删除了范围测试部分。
我没有添加任何依赖项,我只是改变了我使用它们的方式。
预览代码
(如果您使用的是弹性搜索版本< 7.0,则取消注释此代码)
IndexRequest indexRequest = new IndexRequest(
"twitter",
"tweets",
id // this is to make our consumer idempotent
).source(record.value(), XContentType.JSON);
当前代码
IndexRequest indexRequest = new IndexRequest("tweets")
.source(record.value(), XContentType.JSON)
.id(id); // this is to make our consumer idempotent
我使用bulkrequest,并删除了这个错误。