Log4j-slf4j-impl cannot be present with log4j-to-slf4j

The error message “log4j-slf4j-impl cannot be present with log4j-to-slf4j” typically occurs when there is a conflict between the log4j-slf4j-impl and log4j-to-slf4j libraries in a Java project.

The log4j-slf4j-impl library is used to bridge between log4j and the SLF4J logging facade, allowing applications written with log4j to use the SLF4J API. On the other hand, the log4j-to-slf4j library is used to redirect log4j logging to SLF4J.

The problem arises when both libraries are present in the classpath, leading to conflicts and inconsistencies in the logging behavior. In order to resolve this issue, you need to decide which logging framework you want to use – either log4j or SLF4J – and make sure that only the relevant dependencies are included in the project.

Here’s an example of how you can resolve this conflict by excluding the log4j-slf4j-impl dependency while using the log4j-to-slf4j library:

    
      <dependency>
        <!-- Exclude log4j-slf4j-impl to prevent conflicts -->
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-to-slf4j</artifactId>
        <version>2.17.1</version>
        <exclusions>
          <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
    
  

Similar post

Leave a comment