3

I'm using Spring Boot 1.5.8. SLF4J doesn't seem to be working for me. Here is what I get in the console at the beginning of a project run. The below error shows appears :

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.

START : EDIT 1 :

My console doesn't log the below:

LOGGER.error("ERROR");
LOGGER.error("WARN");
LOGGER.error("INFO");
LOGGER.error("DEBUG");
LOGGER.error("TRACE");

All I get is those three lines of error(warning) as the first lines in my console.

END : EDIT 1 :

I looked up the internet and found multiple issues titled as this,tried the suggestions but that didn't resolve my issue. I came across this stackoverflow question where Konstantinos hints at the possibility of presence of multiple slf4j dependencies. Other solutions on the internet also suggested that; even the official site confirmed that to be the reason for such error.

Also checked my effective pom just to eliminate this possibility and found that my effective pom does contain all the 4 dependencies that he mentioned. The general suggestion is that the pom should contain only one of these four dependencies. But , since this was all handled by Spring boot, I'm a bit confused as to how do I go about resolving this, because I never exclusively included any slf4j dependency in my pom. So I'm unable to follow the suggestion.

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codingethics.flightreservation</groupId>
    <artifactId>flightreservation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>flightreservation</name>
    <description>Flight Reservation Application</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.properties :

spring.datasource.url=jdbc:mysql://localhost:3306/reservation
spring.datasource.username=root
spring.datasource.password=password

spring.jpa.show-sql=true

spring.mvc.view.prefix=/WEB-INF/jsps/
spring.mvc.view.suffix=.jsp

server.context-path=/flightreservation

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<myusername>
spring.mail.password=<mypwd>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

Why does this dependency-conflict(if at all that's the case here) happen in a Spring Boot project. Isn't Spring Boot meant to be looking after these beforehand.

Any sort of help/ guidance is highly appreciated.

START : EDIT for @Antot

I get NoClassDefFoundError.(followed steps as advised by you) :

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:179)
    at com.codingethics.flightreservation.FlightreservationApplication.main(FlightreservationApplication.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

END : EDIT for @Antot

Asif Kamran Malick
  • 2,409
  • 3
  • 25
  • 47
  • 1
    You included `iText` which isn't a spring boot managed dependency and whatever that pulls in will be added to your classpath. – M. Deinum Apr 16 '18 at 17:52
  • Thanks. I understand that. But how do I resolve this. I need iText too in my project. – Asif Kamran Malick Apr 16 '18 at 18:06
  • just an update: I commented out the itext dependency and all related loc. still no luck. Its same as before. So , is it really the iText that's causing this issue? – Asif Kamran Malick Apr 16 '18 at 18:22
  • check [this](https://stackoverflow.com/questions/11916706/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder-error) – Amit K Bist Apr 16 '18 at 20:58
  • @Amit.. Went through the post and also the original bug reported on the eclipse site.If I understood those correctly, then their concern was just the presence of those three lines in the console. Whereas, my concern is that logging isn't working in my case. I've added few more details to my question. Please check. – Asif Kamran Malick Apr 16 '18 at 22:39
  • 2
    Use `mvn dependency:tree` to show what is pulling in the dependencies. You might also want to try to re-download the dependencies (maybe a borked jar has been downloaded). Use `mvn dependency:purge-local-repository` and then `mvn package` to re-download the dependencies. – M. Deinum Apr 17 '18 at 06:39
  • 2
    @Asif I've tried to make a build with your initial pom and the issue is not reproduced. I agree with M.Deinum that there might be inconsistencies with dependencies in your local repository. – Antot Apr 17 '18 at 07:36
  • @M. Deinum : Thanks a ton. I executed those two commands for my project and that worked. logging now works like a charm. This not only resolved my issue, but taught me few things.I realized the power of these two short commands. The 3 lines of slf4j warning don't show up anymore. I've seen many people suggesting to delete the complete local repo in most cases as a final way out. I never did that as just deleting the specific dependencies resolved my issues.but that didn't work in this case. Will save your suggestion for future reference. Please post as answer, so I can mark as accepted. – Asif Kamran Malick Apr 17 '18 at 14:53
  • @Antot I would liek to thank you for your great help. Yes, the problem seemed to be with my local repo. I executed `mvn dependency:purge-local-repository` under my project directory and then `mvn package`. And everything was fine again. – Asif Kamran Malick Apr 17 '18 at 15:06

1 Answers1

4

You might first want to check if something that isn't managed by Spring Boot is pulling in additional dependencies. You can do this with mvn dependency:tree to see what is pulling in what.

If that doesn't give a hint in the right direction it might be that one of your downloaded dependencies got borked. You might want to purge the local repository. You can do this with mvn dependency:purge-local-repository to remove all your dependencies from your local maven repository.

Then when doing mvn package (or another task that will compile and build the jar) will (re)download the jars and hopefully in a correct way without corruption.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • Just reiterating: This resolved my issue. Two magical commands : `mvn dependency:purge-local-repository` and `mvn package`. Would like to add a point in case someone else comes across this same issue : Running the purge command will also try to download few dependencies before actually purging. Check the section **Transitive dependency resolution** [here](https://maven.apache.org/plugins/maven-dependency-plugin/examples/purging-local-repository.html) – Asif Kamran Malick Apr 17 '18 at 19:22