Failed to load class “org.slf4j.impl.StaticLoggerBinder” error in java project?

故事扮演 提交于 2019-12-10 03:50:48

问题


I'm getting Failed to load class "org.slf4j.impl.StaticLoggerBinder" error.I want to write the logger in to a file.so i used log4j.jar and am using apache tomcat server.

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.

回答1:


First of all. Regarding the dependencies.

In order to add SLF4J you must put ONE and only ONE of these dependencies in your pom.xml. It depends on what implementation you choose to use. Every dependency you add in the pom.xml is added automatically in the classpath. If one of the below dependencies are provided by another dependency then you can omit it. Dont forget that you must include only one even if the dependency is provided by another dependency. Notice that i have omitted the version from the dependencies. Use the latest available.

<dependency>
   <groupId>ch.qos.logback</groupId>
   <artifactId>logback-classic</artifactId>
   <version></version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-simple</artifactId>
   <version></version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version></version>
   <scope>compile</scope>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-jdk14</artifactId>
   <version></version>
   <scope>compile</scope>
</dependency>

Now regarding the annoying error you are getting when building your maven project. If after having only one of the above dependencies you still get the SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". then you are facing a bug from m2e.

Eclipse Juno and Indigo, when using the bundled maven version(m2e), are not suppressing the message SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". This behaviour is present from the m2e version 1.1.0.20120530-0009 and onwards.

Although, this is indicated as an error your logs will be saved normally. The highlighted error will still be present until there is a fix of this bug. More about this in the m2e support site.

The current available solution is to use an external maven version rather than the bundled version of Eclipse. You can find about this solution and more details regarding this bug in the question below which i think describes the same problem you are facing.

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




回答2:


you need to add the slf4j jar or dependency to your project




回答3:


There is no explicit example of how to use log4j and sl4j together in the manual. But there are plenty of unofficial ones, I like this one: http://www.javavillage.in/slf4j-with-log4j.php

Note only two maven deps. There is no explicit log4j dep, it is loaded automatically.

Also (and it costed me 1 hour and 14 minutes) some versions of sl4j are not stable, and it matters. Version 1.8-beta (yeah.. beta) doesn't work. So I used 1.7.13 and it works.

So, check for maven deps, it should be like this:

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

Default configuration location is resources folder. E.g. for main sources:

project/src/main/resources

or for tests:

project/src

Cheers!




回答4:


Please import slf4j-api-1.6.6.jar for your project. 1.write following code in your java class

private Logger logger = LoggerFactory.getLogger(this.getClass());

2.import following:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

3.now you can use logger.info(); to print anything.



来源:https://stackoverflow.com/questions/16190880/failed-to-load-class-org-slf4j-impl-staticloggerbinder-error-in-java-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!