Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to load class "org.slf4j.impl.StaticLoggerBinder" error in java project? [duplicate]

Tags:

java

log4j

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.
like image 700
user2210071 Avatar asked Apr 24 '13 11:04

user2210071


3 Answers

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!

like image 186
Stepan Dyatkovskiy Avatar answered Nov 09 '22 04:11

Stepan Dyatkovskiy


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

like image 41
Konstantinos Margaritis Avatar answered Nov 09 '22 04:11

Konstantinos Margaritis


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

like image 38
Dima Avatar answered Nov 09 '22 02:11

Dima