我的应用程序将部署在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错误代码

加载org.slf4j.impl.StaticLoggerBinder类失败 当org.slf4j.impl。无法将StaticLoggerBinder类装入内存。当在类路径上找不到合适的SLF4J绑定时,就会发生这种情况。在类路径上放置一个slf4j-nop.jar、slf4j-log4j12.jar、slf4j-jdk14.jar或logback-classic.jar应该可以解决问题。

注意slf4j-api 2.0版本。x和以后使用ServiceLoader机制。后端,如logback 1.3和以后的目标slf4j-api 2。x,不附带org.slf4j.impl.StaticLoggerBinder。如果您放置了一个针对slf4j-api 2.0的日志记录后端。X,在类路径上需要slf4j-api-2.x.jar。参见相关的faq条目。

从SLF4J 1.6版开始,在没有绑定的情况下,SLF4J将默认为一个no-operation (NOP)记录器实现。

如果您负责打包应用程序,并且不关心日志记录,那么将slf4j-nop.jar放在应用程序的类路径上将消除此警告消息。请注意,嵌入式组件(如库或框架)不应该声明依赖于任何SLF4J绑定,而只依赖于SLF4J -api。当库在SLF4J绑定上声明编译时依赖项时,它将该绑定强加给最终用户,从而否定了SLF4J的目的。

其他回答

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

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

这是给那些从谷歌搜索来到这里的人。

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

Or

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

我没有添加任何依赖项,我只是改变了我使用它们的方式。

预览代码

(如果您使用的是弹性搜索版本< 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,并删除了这个错误。

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

这解决了我的问题。

我添加了这个依赖来解决这个问题:

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