springboot2.x integrated log4j2 log

spring-boot2.x integrates log4j2

spring-boot2.x uses logback as the default log processing library

Dependent library

  1. maven way:
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-log4j2</artifactId>
	</dependency>
  1. Eliminate conflict dependencies
<exclusions>
	<exclusion>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-logging</artifactId>
	</exclusion>
</exclusions>

Configuration file

  1. 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

  1. 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>

Guess you like

Origin blog.csdn.net/eguid/article/details/97800942
Recommended