springboot2.x只需两步快速整合log4j2的方法
作者:eguid_1 时间:2023-06-06 20:41:21
前言
本文详细介绍如何使用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中,是不是很简单。
来源:https://juejin.cn/post/6956808916730642446
标签:springboot,整合,log4j2
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
点九图片的显示内容区域应作何理解
2022-12-27 12:24:30
![](https://img.aspxhome.com/file/2023/1/91691_0s.png)
Mybatis-Plus自动填充更新操作相关字段的实现
2022-01-14 20:43:01
深入理解TextView实现Rich Text--在同一个TextView设置不同字体风格
2023-05-25 04:52:01
![](https://img.aspxhome.com/file/2023/8/84738_0s.gif)
Android仿京东搜索框渐变效果
2022-09-08 19:57:05
![](https://img.aspxhome.com/file/2023/0/114350_0s.gif)
Java对字符串进行加密解密
2022-10-09 05:39:48
Android如何在原生App中嵌入Flutter
2022-03-11 17:06:06
![](https://img.aspxhome.com/file/2023/2/122132_0s.jpg)
C# PictureBox控件方法参数及图片删除重命名上传详解
2023-02-04 17:59:08
![](https://img.aspxhome.com/file/2023/1/106331_0s.png)
SpringBoot+SpringCache实现两级缓存(Redis+Caffeine)
2023-10-02 05:20:58
![](https://img.aspxhome.com/file/2023/5/97965_0s.png)
Android悬浮对话框(即点即关对话框)实现代码
2023-01-01 04:53:54
![](https://img.aspxhome.com/file/2023/2/139222_0s.jpg)
Android中的Bitmap缓存池使用详解
2023-02-23 15:01:15
Java字符串的intern方法有何奥妙之处
2022-07-21 13:38:16
![](https://img.aspxhome.com/file/2023/8/118828_0s.png)
Spring Boot应用的极速部署脚本示例代码
2021-07-06 21:22:28
Java数据结构之红黑树的真正理解
2022-07-16 01:36:16
![](https://img.aspxhome.com/file/2023/9/78369_0s.png)
C语言文件操作之fread函数详解
2023-07-06 18:24:15
![](https://img.aspxhome.com/file/2023/7/103077_0s.jpg)
JDBC基础教程
2021-10-26 03:47:09
Android使用Retrofit上传文件功能
2022-08-28 08:42:38
Android学习笔记之蓝牙功能
2022-05-19 05:07:44
![](https://img.aspxhome.com/file/2023/4/101874_0s.jpg)
OpenXml读写Excel实例代码
2023-09-10 18:11:22
java中如何获取相关参数
2023-11-17 20:12:39
打印Java程序的线程栈信息方式
2021-11-02 19:00:28