【问题标题】:SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". in a Maven Project [duplicate]SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。在 Maven 项目中[重复]
【发布时间】:2012-09-09 14:56:40
【问题描述】:

我依赖于 SLF4J。我收到此错误:

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.

这是我的 Maven 条目:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.6</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.6</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>

问题出在哪里?


编辑:没有 log4j 依赖我得到以下异常

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org.slf4j.impl.StaticLoggerBinder
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:121)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:111)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:268)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:241)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:254)
    at org.apache.bval.jsr303.ConfigurationImpl.<clinit>(ConfigurationImpl.java:50)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:228)
    at org.apache.bval.jsr303.ApacheValidationProvider.createGenericConfiguration(ApacheValidationProvider.java:66)
    at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:173)
    at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:50)
    at com.daimler.zv9.basic.entity.AbstractEntity.<clinit>(AbstractEntity.java:73)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:228)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:195)
    at java.lang.Class.forNameImpl(Native Method)
    at java.lang.Class.forName(Class.java:168)

EDIT2:这是我得到的一个示例 MAVEN 树

[INFO] com.myCompany.abc:ABC_Document:ejb:0.0.1-SNAPSHOT
[INFO] +- com.myCompany.abc:ABC_Basic:jar:0.0.1-SNAPSHOT:compile
[INFO] +- com.myCompany.iap:javax.j2ee:jar:5.0.0:provided
[INFO] +- com.myCompany.iap:logging:jar:5.0.0:provided
[INFO] +- com.myCompany.iap:util:jar:5.0.0:provided
[INFO] +- junit:junit:jar:4.8.2:test
[INFO] +- javax.servlet:jstl:jar:1.2:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.6.6:compile
[INFO] +- org.slf4j:slf4j-simple:jar:1.6.6:compile
[INFO] \- org.mockito:mockito-all:jar:1.9.0:test

【问题讨论】:

  • @Ceki 也许你有想法?
  • 这是一个独立的程序还是部署在某个服务器上?
  • @basiljames 我收到 mvn openjpa:sql 命令的错误。它没有安装在任何地方。
  • 看起来实际运行的 slf4j-api 版本在其类路径中找不到 slf4j-log4j12。你确定你的类路径中没有另一个 slf4j-api.jar 吗?
  • 在 WAS 7 中,我记得通过设置一个反转类路径遍历的选项来解决类似的问题,将应用程序特定的库放在首位。

标签: java maven log4j slf4j


【解决方案1】:

我假设您使用 Eclipse 作为您的开发环境。

Eclipse Juno、Indigo 和 Kepler 在使用捆绑的 maven 版本 (m2e) 时不会抑制消息 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"。此行为从 m2e 版本 1.1.0.20120530-0009 及更高版本开始存在。

虽然,这表示为错误,您的日志将正常保存。在修复此错误之前,突出显示的错误仍将存在。更多信息请关注m2e support site

当前可用的解决方案是使用外部 maven 版本而不是 Eclipse 的捆绑版本。您可以在下面的问题中找到有关此解决方案以及有关此错误的更多详细信息,我相信该问题描述了您面临的相同问题。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". error

【讨论】:

  • 如果使用外部maven版本不行,建议删除本地maven仓库的内容。
  • 我在 Intellij IDEA 上遇到了同样的问题,同样的解决方案(“使用外部 maven 版本而不是捆绑版本”)对我有用。
【解决方案2】:

删除

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency> 

slf4j-log4j12slf4j 的 log4j 绑定,您不需要添加另一个 log4j 依赖项。

已添加
log4j.properties 中提供 log4j 配置并将其添加到您的类路径中。有样例配置here

或者您可以将绑定更改为

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

如果由于某些依赖项需要它而配置 slf4j。

【讨论】:

  • 我删除了它。我在 org.slf4j.LoggerFactory.bind(LoggerFactory.java:121) at org.slf4j.LoggerFactory.performInitialization 处收到此异常“无法实例化 SLF4J LoggerFactory 报告异常:java.lang.NoClassDefFoundError: org.slf4j.impl.StaticLoggerBinder (LoggerFactory.java:121) (LoggerFactory.java:111) 在 org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:268)'
  • 一个类似的post
  • 当 slf4j-log4j 被声明为依赖时,添加 log4j 作为依赖是不必要的,但它也不应该受到伤害。此外,log4j.properties 与所提出的问题无关。
  • 感谢为我工作
  • 谢谢!它帮助了我。
【解决方案3】:

我在 IntelliJ 中遇到了这个问题,并通过在我的 pom 中添加以下内容来修复它:

<!-- logging dependencies -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
        <exclusions>
            <exclusion>
                <!-- Defined below -->
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>

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

【讨论】:

  • 更好的解决方法是使用一个dependencyManagement 元素来确定slf4j-api 的版本
  • 我使用的是 spring boot 15。这个解决方案对我有用
【解决方案4】:

你提到的信息很清楚:

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 找不到绑定,并决定默认为 NOP 实现。在您的情况下,当 LoggerFactory 类加载到内存中时,slf4j-log4j12.jar 不知何故不可见,这无疑是 非常 奇怪的。 “mvn dependency:tree”告诉你什么?

各种依赖声明甚至可能不是这里的直接原因。我强烈怀疑 slf4j-api.jar 的 1.6 之前的版本在你不知情的情况下被部署。

【讨论】:

  • 这就是依赖关系:树告诉:[INFO] [INFO] --- maven-dependency-plugin:2.5.1:tree (default-cli) @ GEM_Web --- [INFO] com.mycompany.proj:My_Web:war:0.0.1-SNAPSHOT ... [INFO] +- javax.servlet:jstl:jar:1.2:provided [INFO] +- org.slf4j:slf4j-api:jar:1.6.6:compile [INFO] +- org.slf4j:slf4j-simple:jar:1.6.6:compile [INFO] \- org.mockito:mockito-all:jar:1.9.0:test
  • 以上内容不太清晰。 IMO,它应该移到问题部分。
  • 感谢您的提示。我更新了问题
猜你喜欢
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多