Fading Coder

An Old Coder’s Final Dance

You are here: Home > Tech > Content

Resolving `java.lang.IllegalStateException` in Logback configuration during Log4j2 migration

Tech 3

Understanding the Exception java.lang.IllegalStateException: Logback configuration error detected

Overview

This article provides a detailed exploration of the exception java.lang.IllegalStateException encountered during the initialization of a logging system in a Spring Boot project configured too use Logback. The example scenario involves replacing Logback with Log4j2 as the logging framework.

Error Details

The stack trace below summarizes the issue:

java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.spi.Interpreter@3:16 - no applicable action for [appenders], current ElementPath is [[configuration][appenders]]
...

Upon reviewing the first lines:

Logging system failed to initialize using configuration from 'classpath:log4j2-dev.xml'
java.lang.IllegalStateException: Logback configuration error detected:

It appears the issue stems not from the XML configuration file log4j2-dev.xml directly but from conflicting dependencies in the project's pom.xml setup.

Investigation

Here is the relevant portion of the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</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-log4j2</artifactId>
</dependency>

Upon closer examination, it's evident that spring-boot-starter-web indirectly includes Logback dependencies, causing conflicts.

Dependency Conflict Diagram

Here is a conceptual dependency diagram:

  • Web (spring-boot-starter-web): Includes Logback.
  • MyBatis (spring-boot-starter-jdbc): Includes Logback.

These conflict with the explicitly defined Log4j2 dependency spring-boot-starter-log4j2.

Solutions

Resolution Option 1: Exclude Logback Globally

To prevent conflicts, exclude Logback across all dependencies:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Resolution Option 2: Targeted Exclusions

For selective exclusions, targeting specific dependencies can also resolve the issue:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Summary

By examining dependency inclusions and selectively excluding the conflicting spring-boot-starter-logging, issues with the logging initialization processs can be avoided, enabling proper integration of Log4j2 in the Spring Boot application.

Tags: Java

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.