<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
dependencies {
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
}
Spring Boot supports Log4j 2 for logging configuration if it is on the classpath. If you use the starters for assembling dependencies, you have to exclude Logback and then include Log4j 2 instead.
<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>
mvn dependency:tree
[INFO] +- org.springframework.boot:spring-boot-starter-log4j2:jar:2.7.12:compile
[INFO] | +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.17.2:compile
[INFO] | | \\- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] | +- org.apache.logging.log4j:log4j-core:jar:2.17.2:compile
[INFO] | +- org.apache.logging.log4j:log4j-jul:jar:2.17.2:compile
[INFO] | \\- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
If you don't want to depend on the Log4j2 API and instead want to use SLF4J, that is possible as well. Assuming that our code looks like the following:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Log4j2Test { private static final Logger logger = LoggerFactory.getLogger(Log4j2Test.class); public Log4j2Test(){ logger.info( "Hello World!" ); } }If existing components use SLF4J and you want to have this logging routed to Log4j 2, then add the following but do not remove any SLF4J dependencies.
SLF4J Bridge<dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.20.0</version> </dependency> </dependencies>Due to a break in compatibility in the SLF4J binding, as of release 2.19.0 two SLF4J to Log4j Adapters are provided.
log4j-slf4j-implshould be used with SLF4J 1.7.x releases or older.log4j-slf4j2-implshould be used with SLF4J 2.0.x releases or newer.Applications that take advantage of the Java Module System should use SLF4J 2.0.x and log4j-slf4j2-impl. As of release 2.19.0 the log4j-slf4j18-impl module targeting the unreleased SLF4J 1.8.x series has been removed.
理解为输出源,日志输出目的地,负责日志的输出
Appenders are responsible for delivering LogEvents to their destination. Every Appender must implement the Appender interface. Most Appenders will extend AbstractAppender which adds Lifecycle and Filterable support. Lifecycle allows components to finish initialization after configuration has completed and to perform cleanup during shutdown. Filterable allows the component to have Filters attached to it which are evaluated during event processing.
Appenders usually are only responsible for writing the event data to the target destination. In most cases they delegate responsibility for formatting the event to a layout. Some appenders wrap other appenders so that they can modify the LogEvent, handle a failure in an Appender, route the event to a subordinate Appender based on advanced Filter criteria or provide similar functionality that does not directly format the event for viewing.
Appenders always have a name so that they can be referenced from Loggers.