springboot2.x只需两步快速整合log4j2的方法
作者:eguid_1
这篇文章主要介绍了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中,是不是很简单。
到此这篇关于springboot2.x只需两步快速整合log4j2的方法的文章就介绍到这了,更多相关springboot2.x整合log4j2内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
