maven 配置jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency> <!-- 加上这个才能辨认到log4j2.yml文件 -->
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
application-dev.yml配置文件
logging:
config: classpath:log4j2-dev.yml
log4j2-dev.yml配置文件
#参考文档:
#官网:https://logging.apache.org/log4j/2.x/manual/configuration.html#YAML
#https://www.jianshu.com/p/4ac14b9f51d2?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
#http://shmilyaw-hotmail-com.iteye.com/blog/2410764
#
Configuration:
status: WARN #log4j2自身组件的日志级别 Properties: # 定义全局变量
Property:
- name: zhx.log.level
value: debug
- name: springframework.log
value: info
- name: org.mybatis
value: debug
- name: log.path
value: logs/owg_server/
- name: project.name
value: owg_server
Appenders:
Console: #输出到控制台
name: CONSOLE
target: SYSTEM_OUT
ThresholdFilter:
level: ${zhx.log.level}
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n" RollingFile: #输出到文件
name: ROLLING_FILE
ThresholdFilter:
level: ${zhx.log.level}
onMatch: ACCEPT
onMismatch: DENY
fileName: ${log.path}/${project.name}.log
filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}.log"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
Policies: #触发策略
SizeBasedTriggeringPolicy : #基于日志文件大小触发
size: "50MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: ${zhx.log.level}
additivity: false
AppenderRef:
- ref: CONSOLE
Logger:
- name: com.zhx
level: ${zhx.log.level}
additivity: false
AppenderRef:
- ref: CONSOLE
- name: org.springframework
level: ${springframework.log}
additivity: false
AppenderRef:
- ref: CONSOLE
- name: org.mybatis
level: ${org.mybatis}
additivity: false
AppenderRef:
- ref: CONSOLE