引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<!-- remove log4j -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
配置
yml中添加
logging: # log4j2 config
config: classpath:log4j2.yml
level:
org.springframework.boot.autoconfigure: ERROR
org.apache.http.impl.conn: ERROR
log4j2.yml
Configuration:
name: @service.name@_logger
Properties:
Property:
- name: log.path
value: log
- name: app.name
value: @service.name@
- name: log.pattern
value: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %l: %msg%n"
Appenders:
Console:
name: console
target: SYSTEM_OUT
PatternLayout:
pattern: ${log.pattern}
RollingFile:
name: running_log
fileName: ${log.path}/${app.name}-running.log
filePattern: "logs/archive/running.log.%d{yyyy-MM-dd-hh-mm}.gz"
PatternLayout:
charset: utf-8
pattern: ${log.pattern}
Filters:
ThresholdFilter:
- level: info
onMatch: ACCEPT
onMismatch: DENY
Policies:
SizeBasedTriggeringPolicy:
size: 500 MB
DefaultRollOverStrategy:
max: 200
Loggers:
AsyncRoot:
level: info
includeLocation: true
AppenderRef:
- ref: console
- ref: running_log