menteith
menteith

Reputation: 678

Using log4j2 and log4j at the same time in Spring project

I've a Spring Boot Applications that uses third-party library. That library uses log4j as its logger. When there is no log4j in classpath, my application fails to start because that third-party library throws an exception pointing to missing log4j. To handle this, I decided to use log4j2 instead of Spring's default logback. To do so, I put the following in pom.xml:

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

Additionally, to allow for that thrid-party library to work, I also added:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>compile</scope>
    </dependency>

Now,the third-party library works, however, everything is logged twice. How do I to handle this?

Upvotes: 0

Views: 986

Answers (1)

Marco Behler
Marco Behler

Reputation: 3724

Instead of adding the log4j dependency to your project, add the log4j 1.x bridge to your project, which will make sure your library that depends on log4j actually works with Log4j2 without knowing it.

So, make sure to remove the old log4j 1.2 dependency and add this one instead:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.13.1</version>
</dependency>

Upvotes: 2

Related Questions