Caused by: org.apache.logging.log4j.loggingexception: log4j-slf4j-impl cannot be present with log4j-to-slf4j

Answer:

The error message caused by: org.apache.logging.log4j.LoggingException: log4j-slf4j-impl cannot be present with log4j-to-slf4j occurs when there is a conflict between two Log4j libraries: log4j-slf4j-impl and log4j-to-slf4j.

The log4j-slf4j-impl library is an implementation of the SLF4J logging facade for Log4j 2, while the log4j-to-slf4j library bridges Log4j 2 API calls to SLF4J. Having both of these libraries present in the classpath at the same time can result in conflicts and errors.

To resolve this issue, you need to exclude one of the libraries depending on your requirements.

Example:

If you want to use Log4j 2 as the logging implementation, you should exclude the log4j-to-slf4j library.

<!-- Exclude log4j-to-slf4j in Maven -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>[version number]</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>

On the other hand, if you want to use SLF4J with Log4j 2, you should exclude the log4j-slf4j-impl library.

<!-- Exclude log4j-slf4j-impl in Maven -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>[version number]</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Make sure to replace [version number] with the desired version of each library.

Similar post

Leave a comment