springboot2.x只需两步快速整合log4j2

前言

本文详细介绍如何使用spring-boot2.x快速整合log4j2日志框架。

spring-boot2.x使用logback作为默认日志处理库,因此我们除了要引用log4j2之外,还要去除logback的依赖

1、依赖库

maven方式:

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-log4j2</artifactId>
	</dependency>
去除冲突依赖
复制代码
<exclusions>
	<exclusion>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-logging</artifactId>
	</exclusion>
</exclusions>
复制代码

配置文件
配置文件路径
配置文件放在根目录或者resource目录都可以,也可以在application.properties中配置指定路径:
logging.config=classpath:log4j2.xml

2、配置

<?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>
复制代码

到此为止,我们就已经将log4j2应用到了springboot中,是不是很简单。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享