NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder (with SLF4J, Logback and Spring Boot)
Asked Answered
M

2

3

Since of nowhere I getting the error message that the class StaticLoggerBinder was not found. I guess it have something to do with the Spring-Boot API. Any solutions to the problem?

pom.xml:

      <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.5.5</version>
            </dependency>
        <!-- json request -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20210307</version>
        </dependency>

        <!-- Log -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.7</version>
        </dependency>
        <!-- logger -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.4.8</version>
        </dependency>

logback.xml:

<configuration>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/reportsystem.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/reportsystem.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="CUSTOM" class="de.netzmelden.reportsystem.logging.CustomConsoleAppender">
    
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{1} - %msg%n</pattern>
        </encoder>
    </appender>


    <root level="info">
        <appender-ref ref="FILE"/>
        <appender-ref ref="CUSTOM"/>
    </root>

</configuration>

exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:293)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:118)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:232)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:213)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:76)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$starting$0(SpringApplicationRunListeners.java:53)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
    at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:53)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:329)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332)
    at de.netzmelden.reportsystem.ReportSystem.main(ReportSystem.java:72)
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 17 more

I've tried using older versions of slf4j-api and logback-classic, but with no success.

The question is why does my program need StaticLoggerBinder and throw an exception about it when I'm using the newest slf4j-api and logback-classic versions which shouldn't care about it?

Any suggestions on how to solve the problem would be appreciated!

Microcrystalline answered 2/1 at 20:40 Comment(2)
slf4j-api is the dependency that defines the interfaces that are used and provides them for own implementations. You also and/or instead need to include a real implementation that actually contains executeable logging logic. So add the reall logger as wellMotivate
@Motivate that's not the issue, logback-classic is an implementation.Cyclo
D
2

Spring Boot 2.5.5 depends on logback 1.2.x and SLF4J 1.x.

SLF4J implementation significantly changed between 1.x and 2.x (and as a consequence logback 1.2.x vs. 1.3.x/1.4.x) so that some usages are not compatible anymore. Typically usage of StaticLoggerBinder is not possible anymore.

The 1st version of Spring Boot compatible with SLF4J 2.x and logback 1.3.x/1.4.x is the 3.0.0.

You need to either stay with logback 1.2.x and SLF4J 1.x, or also upgrade Spring Boot to 3.x. I would recommend the latter obviously.


See also:

Digiovanni answered 2/1 at 21:5 Comment(0)
B
0

I had exactly the same problem in my Spring boot 2 project with version 2.7.18, and I basically solved it by removing the org.slf4j.slf4j-api dependency in the pom and downgrading the version of the dependency ch.qos.logback.logback-classic, the pom ended up with these dependencies basically:

<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-logging</artifactId>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.13</version>
</dependency>
Bulla answered 18/10 at 22:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.