javaspring-bootlogbackslf4j

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


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!


Solution

  • 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: