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

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


当前回答

这里有相当多的答案建议向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>

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

其他回答

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

正如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)

     <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>

将上述依赖项放在pom.xml文件中

可以使用相同的版本来解决这个问题。我试过了,而且解决了

< >的依赖 < groupId > org.slf4j < / groupId > < artifactId > slf4j-api < / artifactId > <版本> 1.7.5 > < /版本 < / >的依赖 < >的依赖 < groupId > org.slf4j < / groupId > < artifactId > slf4j-log4j12 < / artifactId > <版本> 1.7.5 > < /版本 < / >的依赖

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

这解决了我的问题。