一、引入依赖
SpringBoot默认使用logback的日志框架,所以需要排除logback,否则会有依赖冲突
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- yml转xml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
二、配置application.yml
logging:
config: classpath:log4j2.yml
三、配置log4j2.yml
# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
# intLevel值依次为0,100,200,300,400,500,600,700
# intLevel 值越小,级别越高
Configuration:
# 日志框架本身的输出日志级别
status: WARN
# 自动加载配置文件的间隔时间,不低于5秒
monitorInterval: 5
Properties: # 定义全局变量
Property:
# 缺省配置(用于开发环境)。其他环境需要在VM参数中指定,如下:
# 测试:-Dlog.level.console=warn -Dlog.level.xxx=trace
# 生产:-Dlog.level.console=warn -Dlog.level.xxx=info
- name: log.level.console
value: info
- name: log.path
value: ./${project.name}_log
- name: project.name
value: your_project_name
- name: log.pattern
value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n"
Appenders:
Console: # 输出到控制台
name: CONSOLE
target: SYSTEM_OUT
PatternLayout: # 日志消息格式
pattern: ${log.pattern}
# 滚动日志
RollingFile:
- name: ROLLING_FILE
fileName: ${log.path}/daily/${project.name}.log # 输出文件的地址
filePattern: "${log.path}/daily/$${date:yyyy-MM-dd}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz" # 文件生成规则
PatternLayout:
pattern: ${log.pattern}
Filters:
# 一定要先去除不接受的日志级别,然后获取需要接受的日志级别
ThresholdFilter: # 日志级别过滤器
- level: error # 日志级别
onMatch: DENY # 高于的拒绝
onMismatch: NEUTRAL # 低于的交给下一过滤器
- level: info
onMatch: ACCEPT
onMismatch: DENY
Policies:
SizeBasedTriggeringPolicy: # 日志拆分规则
size: "10MB"
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 单目录下,文件最多20个,超过会删除最早之前的
max: 20
# 错误日志
- name: EXCEPTION_ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/exception/${project.name}_exception.log
filePattern: "${log.path}/exception/$${date:yyyy-MM-dd}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: error
# onMatch="ACCEPT" 匹配该级别及以上
# onMatch="DENY" 不匹配该级别及以上
# onMismatch="ACCEPT" 表示匹配该级别以下的级别
# onMismatch="DENY" 不表示匹配该级别以下的级别
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: ${log.pattern}
Policies:
SizeBasedTriggeringPolicy: # 日志拆分规则
size: "10MB"
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
# 警告日志
- name: WARN_ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/warn/${project.name}_warn.log
filePattern: "${log.path}/warn/$${date:yyyy-MM-dd-dd}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
ThresholdFilter:
level: warn
# onMatch="ACCEPT" 匹配该级别及以上
# onMatch="DENY" 不匹配该级别及以上
# onMismatch="ACCEPT" 表示匹配该级别以下的级别
# onMismatch="DENY" 不表示匹配该级别以下的级别
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: ${log.pattern}
Policies:
SizeBasedTriggeringPolicy: # 日志拆分规则
size: "10MB"
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 20
# 用户行为日志
- name: ROLLING_FILE_USER
fileName: ${log.path}/user/user-${project.name}.log
filePattern: "${log.path}/user/$${date:yyyy-MM-dd}/user-${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: ${log.pattern}
Filters:
# 一定要先去除不接受的日志级别,然后获取需要接受的日志级别
ThresholdFilter:
- level: error
onMatch: DENY
onMismatch: NEUTRAL
- level: info
onMatch: ACCEPT
onMismatch: DENY
Policies:
SizeBasedTriggeringPolicy: # 日志拆分规则
size: "10MB"
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100
Loggers:
Root:
# 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF 选择all则输出全部的日志
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
- ref: EXCEPTION_ROLLING_FILE
- ref: WARN_ROLLING_FILE
Logger:
- name: exception
level: debug
additivity: true #追加
AppenderRef:
- ref: EXCEPTION_ROLLING_FILE
#监听具体包下面的日志
# Logger: 为com.example.aspect包配置特殊的Log级别,方便调试
- name: com.example.aspect
additivity: false
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE_USER
四、使用log4j2
@Slf4j
public class Log4j2TestController {
public void test() {
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
}
}