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

I am running my app with Maven. But I get an error:
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.
App uses log4j:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(App.class);
logger.info("Test 1,2,3");
System.out.println("End of my program");
}
}
pom.xml file includes next dependencies:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
How to solve this problem? I checked solutions in google but it doesnt help...
I tried to add implepentation for example slf4j-simple but nothing changed
errr

The solution did not need to test scope in Slf4j Dependencies because it is probably not available when you run your main method.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
<scope>test</scope> //Remove the test scope
</dependency>
Then it's working fine without any errors.
Thanks.

Related

How do I fix "SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"."

I have an application that has the following error message:
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.
This message occurs after I attempt to run any script for the application. I have found that, as it says, it does not create logs, so I have come up empty handed when something fails.
I am running this in Amazon Linux 2 which is closest to CentOS and Redhat.
I have found the following resources:
This issue is addressed here: http://www.slf4j.org/codes.html#StaticLoggerBinder
I can get the jar I need from here: https://repo1.maven.org/maven2/org/slf4j/slf4j-simple/1.6.2/slf4j-simple-1.6.2.jar
After taking this jar and dropping it into my application's /lib, nothing changes.
Other articles describe adding this file to the class path. In Linux, I get this:
# java -classpath /opt/opendj/lib/slf4j-simple-1.6.2.jar org.slf4j.impl.StaticLoggerBinder
Error: Could not find or load main class org.slf4j.impl.StaticLoggerBinder
# java -jar /opt/opendj/lib/slf4j-simple-1.6.2.jar org.slf4j.impl.StaticLoggerBinder
no main manifest attribute, in /opt/opendj/lib/slf4j-simple-1.6.2.jar
Am I trying to add it to the class path right?
If needed, you can reproduce this issue by doing the following:
Steps to reproduce the behavior:
Install a fresh version of OpenDJ onto CentOS or Amazon Linux2 EC2 Instance. Install java 1.8.0
specifically java-1.8.0-openjdk
Install the server in any configuration, then run a status script.
Expected behavior
Logs should generate and no warning message can be presented.
Firstly, you should take a look here : https://www.slf4j.org/codes.html
Also,you can try adding these SLF4J maven dependencies into your pom and let me know if this works:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.3</version>
</dependency>
Note: Consider that the maven dependencies versions are being updated, then maybe you prefer use the lates version of the dependencies(for example for log4j-api is 2.17.0)
On the other hand, this post could help you as well:
https://mkyong.com/java/log4j2-failed-to-load-class-org-slf4j-impl-staticloggerbinder/
In my case I was getting this error while using MavenCli lib. I had missed to add maven-compat lib in pom.xml. Below is full sample code I had used.
Sample code :
MavenCli mavenCli=new MavenCli();
mavenCli.doMain(new String[]{"clean","install"}, path_of_project_dir, System.out, System.out);
Dependency required :
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.6.3</version>
</dependency>
In my case, I was using the logback in a spring-boot app and faced this issue. It was related to incompatible versions of slf4j and logback which were coming from third party libraries. I excluded all of them and used these:
<!-- logging dependencies-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
${logback.version} and ${slf4j.version} are coming from spring-boot-dependencies-2.X.X.pom file.
I hope it will help :)
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
</dependency>

Why adding a dependency to slf4j-log4j12 does not make slf4j work properly?

I have a project using slf4j. So I need to provide an underlying framework.
I wanted to use log4j, so I specified the following dependency in my pom.xml, as specified here :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.8.0-beta2</version>
</dependency>
But I still have the following error message :
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.
which shouldn't appear anymore, according to this.
If I change the dependency to this one, found on the net, it works :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
Does anyone have an explanation ?
The issue simply came from using a beta version of slf4j. Specifying a dependency to a stable version makes it work correctly :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>

Cannot delete slf4j dependencies

I have a problem with slf4j dependencies.
On POM.xml, i declared this:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
But i meet this error message:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/C:/Users/r.rossi/.m2/repository/org/slf4j/slf4j-
nop/1.5.3/slf4j-nop-1.5.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/Users/r.rossi/.m2/repository/org/slf4j/slf4j-
jdk14/1.5.6/slf4j-jdk14-
1.5.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/Users/r.rossi/.m2/repository/org/slf4j/slf4j-
log4j12/1.7.12/slf4j-log4j12-
1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an
explanation.
SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier.
SLF4J: Upgrade your binding to version 1.6.x.
On those directorties and on maven dependencies i have 1.5 versions but i don't have them on POM.xml file (as you can see), so i don't know how delete them. If i delete the directories, it works but the next esecution the project download again the 1.5 versions. How can i delete those damned 1.5 versions?
As the error stack suggest check the link Multiple Binding.
Also, check Dependency Tree to check which external dependency is trying to include (1.5.5 or earlier version).
Basically, your exclusion code
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
should be inserted inside that dependency which depends on an earlier version 1.5.5.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder", nothing helps

I created default Drools project, tried to launch it, but I have this error:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
what's wrong? I didn't change anything in project. Btw I have no pom.xml file, should I create one?
I checked some solutions, but nothing helps, please help
Add a runtime dependency to logback-classic. Or if you don't use maven/gradle/etc, then add the logback-classic jar in your classpath.
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
See http://www.slf4j.org for more information and other options.

Log4j giving ClassCastException about slf4j LoggerContext

I have configured log4j in my Java application and I am getting this
exception : java.lang.ClassCastException:
org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to
org.apache.logging.log4j.core.LoggerContext.
I already tried after remove log4j-slf4j-impl-2.2.jar and log4j-to-slf4j-2.2.jar but It didn't work it out.
Try this (is the 'standard' way to use a logger):
In your class:
private final static Logger logger = LoggerFactory.getLogger(YourClass.class);
In your methods:
if(logger.isInfoEnabled()) {
logger.info("your log message");
}
And the maven dependencies (pom.xml):
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
You can solve this error by following steps:
Include slf4j-api.jar,log4j-api.jar,slf4j-log4j.jar
and
Use logger of slf4j.

Categories