SpringBoot的日志框架使用、日志框架切换【log4j2】
SpringBoot 整合 log4j2 使用yml的配置
目录
排除logging starter
配置log4j2.yaml
排除logging starter

从上图可以看到,需要从spring-boot-starter-web里面排除logging starter,并加入log4j2 starter
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
springboot会读取到log4j2-spring.xml的配置文件(spring推荐),如果是log4j2.yaml的配置文件,那么需要加入依赖
<!-- 加上这个才能辨认到log4j2.yml文件 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
配置log4j2.yaml
# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
Configuration:
status: warn
monitorInterval: 30
Properties: # 定义全局变量
Property: # 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
#测试:-Dlog.level.console=warn -Dlog.level.xjj=trace
#生产:-Dlog.level.console=warn -Dlog.level.xjj=info
- name: log.level.console
value: debug
- name: log.sql.level
value: trace
- name: log.path
value: ${user.home}/logs
Appenders:
Console: #输出到控制台
name: CONSOLE
target: SYSTEM_OUT
ThresholdFilter:
level: ${sys:log.level.console} # “sys:”表示:如果VM参数中没指定这个变量值,则使用本文件中定义的缺省全局变量值
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
RollingFile: # 输出到文件,超过128MB归档
- name: info
ignoreExceptions: false
fileName: ${sys:log.path}/${date:yyyy-MM}/${date:yyyy-MM-dd}/info.log
filePattern: "${sys:log.path}/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: info
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
- name: debug
ignoreExceptions: false
fileName: ${sys:log.path}/${date:yyyy-MM}/${date:yyyy-MM-dd}/debug.log
filePattern: "${sys:log.path}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: debug
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
- name: error
ignoreExceptions: false
fileName: ${sys:log.path}/${date:yyyy-MM}/${date:yyyy-MM-dd}/error.log
filePattern: "${sys:log.path}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: error
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: info
- ref: debug
- ref: error
# Logger: # 为com.xjj包配置特殊的Log级别,方便调试
# - name: cnki.bdms.module.search.dal
# additivity: true
# level: ${sys:log.sql.level}
# AppenderRef:
# - ref: info
# - ref: debug
# - ref: error
# - ref: CONSOLE
application.yaml指明log4j2.yaml位置
logging:
config: classpath:log4j2.yaml