Java 在Spring项目中同时使用log4j2和log4j

Java 在Spring项目中同时使用log4j2和log4j,java,spring,spring-boot,log4j,log4j2,Java,Spring,Spring Boot,Log4j,Log4j2,我有一个使用第三方库的Spring Boot应用程序。该库使用log4j作为其记录器。当类路径中没有log4j时,我的应用程序无法启动,因为第三方库抛出一个指向缺少log4j的异常。为了处理这个问题,我决定使用log4j2而不是Spring的默认logback。为此,我将以下内容放入pom.xml: <dependency> <groupId>org.springframework.boot</groupId> <

我有一个使用第三方库的Spring Boot应用程序。该库使用
log4j
作为其记录器。当类路径中没有
log4j
时,我的应用程序无法启动,因为第三方库抛出一个指向缺少
log4j
的异常。为了处理这个问题,我决定使用
log4j2
而不是Spring的默认
logback
。为此,我将以下内容放入
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>

org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
spring启动程序日志记录
org.springframework.boot
spring-boot-starter-log4j2
此外,为了让第三方图书馆发挥作用,我还添加了:

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

log4j
log4j
1.2.17
编译

现在,第三方库工作了,但是,所有内容都记录了两次。如何处理这个问题?

不要将log4j依赖项添加到项目中,而是将log4j 1.x桥添加到项目中,这将确保依赖log4j的库在不知情的情况下实际与Log4j2一起工作

因此,请确保删除旧的log4j 1.2依赖项,并添加此依赖项:

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

org.apache.logging.log4j
log4j-1.2-api
2.13.1

不要将log4j依赖项添加到项目中,而是将log4j 1.x桥添加到项目中,这将确保依赖log4j的库在不知情的情况下实际与Log4j2一起工作

因此,请确保删除旧的log4j 1.2依赖项,并添加此依赖项:

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

org.apache.logging.log4j
log4j-1.2-api
2.13.1

如果这不起作用,则您可能仍然以其他方式获取log4j依赖项,或者错误配置日志记录配置,以便记录两次事件-如果您的可加性设置不正确,则可能会发生这种情况。如果这不起作用,则您可能仍然以其他方式获取log4j依赖项,或者错误配置日志记录配置,因此事件记录两次-如果您的可加性设置不正确,则可能发生这种情况。