springboot2.x integrated log4j2 log
spring-boot2.x integrates log4j2
spring-boot2.x uses logback as the default log processing library
Dependent library
- maven way:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
- Eliminate conflict dependencies
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
Configuration file
- Configuration file path
The configuration file can be placed in the root directory or the resource directory, or you can configure the specified path in application.properties:
logging.config=classpath:log4j2.xml
- Configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Appenders>
<!--添加一个控制台追加器-->
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout>
<pattern>[%-5p] %d %c - %m%n</pattern>
</PatternLayout>
</Console>
<!--添加一个文本追加器,文件位于根目录下,名为log.log-->
<File name="File" fileName="logs/log.log">
<PatternLayout>
<pattern>[%-5p] %d %c - %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="DEBUG">
<AppenderRef ref="Console" />
</Root>
<!--把org.springframework包下的所有日志输出到log文件,additivity="false"表示不输出到控制台-->
<Logger name="org.springframework" level="DEBUG" additivity="true">
<AppenderRef ref="File" />
</Logger>
</Loggers>
</Configuration>