[英]Spring Boot - Can't exclude default logging
I have a Spring Boot application and I want to use log4j2 instead of the default logging.我有一个 Spring 引导应用程序,我想使用 log4j2 而不是默认日志记录。
So I've added the log4j2 dependency and exclusion:所以我添加了 log4j2 依赖和排除:
<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>
But I'm still getting the warning:但我仍然收到警告:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/foobar/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/foobar/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.13.3/log4j-slf4j-impl-2.13.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
Also when I run mvn dependency:tree | grep log
另外,当我运行
mvn dependency:tree | grep log
mvn dependency:tree | grep log
I get the following: mvn dependency:tree | grep log
我得到以下信息:
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.3.1.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-to-slf4j:jar:2.13.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-log4j2:jar:2.3.1.RELEASE:compile
[INFO] | +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.13.3:compile
[INFO] | | \- org.apache.logging.log4j:log4j-api:jar:2.13.3:compile
[INFO] | +- org.apache.logging.log4j:log4j-core:jar:2.13.3:compile
[INFO] | +- org.apache.logging.log4j:log4j-jul:jar:2.13.3:compile
Why is there jboss-logging?为什么会有 jboss-logging?
pom.xml: pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>0.8.0-SNAPSHOT</version>
<name>foobar</name>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-starter-data-jpa
also depends on spring-boot-starter-logging
, so it is still added to your class path. spring-boot-starter-data-jpa
也依赖于spring-boot-starter-logging
,所以它仍然被添加到你的 class 路径中。 You can add the exclusion to the spring-boot-starter-data-jpa
as well or preferably add spring-boot-starter
dependency and exclude it from there.您也可以将排除项添加到
spring-boot-starter-data-jpa
中,或者最好添加spring-boot-starter
依赖项并从那里排除它。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
The dependency looks like this spring-boot-starter-web
-> spring-boot-starter
-> spring-boot-starter-logging
(and something similar for the other spring-boot-starter-* dependencies).依赖项看起来像这样
spring-boot-starter-web
-> spring-boot-starter
-> spring-boot-starter-logging
(其他 spring-boot-starter-* 依赖项类似)。 That is why excluding it from the spring-boot-starter
will ensure that the it will be excluded from your build path.这就是为什么将它从
spring-boot-starter
中排除将确保它将从您的构建路径中排除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.