如何解决各种 log4j 的包冲突?

how to resolve packages conflicts on varieties of log4j?

我根据 spring 引导示例编写了这个 pom.xml

当我启动我的应用程序时,出现了这个错误:

SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting WhosebugError.

我是 Java 和 Maven 的新手,并在这里停留了很长一段时间。

我试图在 dependency 中写一些 exclusions。但没有成功。我不知道应该从哪个包中排除哪个包。如果是这样,被依赖的包如何才能正常运行?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <!-- Your own application should inherit from spring-boot-starter-parent -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-samples</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>
    <artifactId>spring-boot-sample-web-secure-jdbc</artifactId>
    <name>Spring Boot Web Secure JDBC Sample</name>
    <description>Spring Boot Web Secure JDBC Sample</description>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
        <name>Pivotal Software, Inc.</name>
        <url>http://www.spring.io</url>
    </organization>
    <properties>
        <main.basedir>${basedir}/../..</main.basedir>
    </properties>
    <dependencies>
        <!-- Compile -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.0.0-alpha2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <!-- Test -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我在您的 pom.xml 上看到没有对 log4j 的依赖。你应该把这个放到你的 pom.xml:

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

发生这种情况是因为 spring-boot-starter 引入了 log4j-over-slf4j 依赖项,而您的其他依赖项之一引入了 log4j。

运行 mvn dependency:tree 并且您应该能够找到哪个依赖项正在引入不需要的 log4j 并使用

将其排除
<exclusions>
  <exclusion> 
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
  </exclusion>
  <exclusion> 
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
  </exclusion>
</exclusions>

它可能只是其中之一,具体取决于您在您身上看到的内容 mvn dependency:tree

如果您更愿意使用 log4j,那么显然只需从 spring-boot-starter 中排除 log4j-over-slf4j

这个问题是由于 logging facadelogging framework 调用之间的无限循环造成的。

https://www.slf4j.org/codes.html#log4jDelegationLoop

我的场景稍微复杂一些。除了这个循环问题。我有两个 日志框架

我最终排除了 log4j-over-slf4jlogback-classic.