天天看点

slf4j报错:SLF4J:Failed to load class "org.slf4j.impl.StaticLoggerBinder".Defaulting to no-operat有效解决办法

运行maven程序时报如下错误:

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报错:SLF4J:Failed to load class "org.slf4j.impl.StaticLoggerBinder".Defaulting to no-operat有效解决办法

以下是 错误的解决办法:

上百度查了下,百度千篇一律复制的博客各种解决办法无非是添加以下这些依赖:

<dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
</dependency>
           

以下是 正确的解决办法:

以上都没效果,点开报错网址进官网一看:

http://www.slf4j.org/codes.html#StaticLoggerBinder

Multiple bindings were found on the class path

SLF4J API is designed to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings.

When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. For example, if you have both slf4j-simple-2.0.0-alpha0.jar and slf4j-nop-2.0.0-alpha0.jaron the class path and you wish to use the nop (no-operation) binding, then remove slf4j-simple-2.0.0-alpha0.jar from the class path.

The list of locations that SLF4J provides in this warning usually provides sufficient information to identify the dependency transitively pulling in an unwanted SLF4J binding into your project. In your project's pom.xml file, exclude this SLF4J binding when declaring the unscrupulous dependency. For example, cassandra-all version 0.8.1 declares both log4j and slf4j-log4j12 as compile-time dependencies. Thus, when you include cassandra-all as a dependency in your project, the cassandra-all declaration will cause both slf4j-log4j12.jar and log4j.jar to be pulled in as dependencies. In case you do not wish to use log4j as the the SLF4J backend, you can instruct Maven to exclude these two artifacts as shown next:

<dependencies>

  <dependency>

    <groupId> org.apache.cassandra</groupId>

    <artifactId>cassandra-all</artifactId>

    <version>0.8.1</version>

    <exclusions>

      <exclusion> 

        <groupId>org.slf4j</groupId>

        <artifactId>slf4j-log4j12</artifactId>

      </exclusion>

      <exclusion> 

        <groupId>log4j</groupId>

        <artifactId>log4j</artifactId>

      </exclusion>

    </exclusions> 

  </dependency>

</dependencies>

NOTE The warning emitted by SLF4J is just that, a warning. Even when multiple bindings are present, SLF4J will pick one logging framework/implementation and bind with it. The way SLF4J picks a binding is determined by the JVM and for all practical purposes should be considered random. As of version 1.6.6, SLF4J will name the framework/implementation class it is actually bound to.

Embedded components such as libraries or frameworks should not declare a dependency on any SLF4J binding but only depend on slf4j-api. When a library declares a compile-time dependency on a SLF4J binding, it imposes that binding on the end-user, thus negating SLF4J's purpose. When you come across an embedded component declaring a compile-time dependency on any SLF4J binding, please take the time to contact the authors of said component/library and kindly ask them to mend their ways.

大致意思是SLF4J发出的警告仅是警告。即使存在多个绑定,SLF4J也会选择一个日志记录框架/实现并与其绑定。

添加这个依赖就完美解决问题:

<dependencies>
  <dependency>
    <groupId> org.apache.cassandra</groupId>
    <artifactId>cassandra-all</artifactId>
    <version>0.8.1</version>

    <exclusions>
      <exclusion> 
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
      </exclusion>
    </exclusions> 

  </dependency>
</dependencies>
           
slf4j报错:SLF4J:Failed to load class "org.slf4j.impl.StaticLoggerBinder".Defaulting to no-operat有效解决办法

总结:多看官网

继续阅读