我的应用程序将部署在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手册所述

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)

其他回答

根据SLF4J官方文档

加载org.slf4j.impl.StaticLoggerBinder类失败 时报告此警告消息 org.slf4j.impl。无法装入StaticLoggerBinder类 内存。当找不到合适的SLF4J绑定时就会发生这种情况 在类路径上。放置一个(且只有一个)slf4j-nop.jar文件, Slf4j-simple.jar、slf4j-log4j12.jar、slf4j-jdk14.jar或 类路径上的Logback-classic.jar应该可以解决这个问题。

只需将这个jar和slf4j api.jar一起添加到类路径中就可以完成工作。 祝你好运

SLF4j是各种日志记录框架的抽象。因此,除了slf4j之外,您还需要在类路径中包含任何日志框架,如log4j或logback(等等)。 要有一个想法,请参考http://logback.qos.ch/manual/introduction.html的第一步

我使用Jena,并将相关依赖项添加到pom.xml

<dependency> 
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.0.13</version>
</dependency>

我尝试添加SLF4J -simple,但它只是消失了“SLF4J:未能加载类”org.slf4j.impl。StaticLoggerBinder " "错误,但logback-classic显示更多详细信息。

官方文件

在Websphere案例中,您有一个较老版本的slf4j-api.jar, 1.4.x。或1.5。X躺在某个地方。您在tcServer上观察到的行为,即故障转移到NOP,发生在slf4j 1.6.0及更高版本上。确保在所有平台上使用slf4j-api-1.6.x.jar,并且在类路径中没有放置slf4j-api的旧版本。

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

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