javaspring-bootmavendependencieslogback-classic

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


I am trying to create a data connection app to integrate with procore and am having trouble with installing my necessary dependencies. This is my pom.xml file:

<?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>

    <!-- Replace with the actual groupId, artifactId, and version of your project -->
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Parent POM for Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.1</version> <!-- Use the appropriate Spring Boot version -->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- Project properties -->
    <properties>
        <java.version>11</java.version>
    </properties>

    <!-- Dependencies -->
    <dependencies>
        <!-- Spring Boot Starter dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Micrometer core -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-core</artifactId>
            <version>1.13.1</version> <!-- Adjust version as per availability -->
        </dependency>

        <!-- Lombok for easier Java development -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <!-- Build plugins -->
    <build>
        <plugins>
            <!-- Spring Boot Maven plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
    </build>

    <!-- Repositories -->
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2/</url>
        </repository>
    </repositories>
</project>

However, the error states that the specific problem would be at the logback-classic dependency which is right here:

<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.5.6</version>
        </dependency>

The error is as follows: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

I have tried utilizing different versions but they all give me the error that: Dependency 'ch.qos.logback:logback-classic:1.2.11' not found and 1.2.11 is a version that should definitely exist. Thank you for any help in advance!


Solution

  • TLDR: Remove both slf4j-api and logback-classic dependencies from your pom. They will be pulled transitively by Spring, in matching versions. (also note that logback-classic pulls slf4j-api, in matching version)

    The reason your current setup does not work is that logback 1.5.6 works with Slf4j 2.x, which differs substantially from 1.7

    See What has changed in SLF4J version 2.0.0?

    Most notably, slf4j-api now relies on the ServiceLoader mechanism to find its logging backend. SLF4J 1.7.x and earlier versions relied on the static binder mechanism which is no longer honored by slf4j-api version 2.0.x. More specifically, during its initialization, the LoggerFactory class will no longer search for the org.slf4j.impl.StaticLoggerBinder class on the class path.

    It you prefer to be explicit about dependency on logback, you could use:

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </dependency>
    

    Here, you are not declaring a version - it is managed in one of Spring's poms using Maven's <dependencyManagement> section. If you want to be extra explicit, do the same with slf4j-api