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

时间:2017-04-15 01:20:27

标签: java spring maven dependencies packages

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

当我开始使用我的应用时,我收到了这个错误:

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

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

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

<?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>

3 个答案:

答案 0 :(得分:4)

这是因为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,那么显然只需从log4j-over-slf4j中排除spring-boot-starter

答案 1 :(得分:0)

此问题是由于日志记录外观日志记录框架之间的无限循环。

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

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

我最终排除 log4j-over-slf4j logback-classic

答案 2 :(得分:-1)

我在你的pom.xml上看到,对log4j没有任何依赖。你应该把它放到你的pom.xml:

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