springboot+log4j2配置日志方式
声明:为什么springboot配置log4j2,一开始我配置的是logback.xml去做日志处理,之后项目配置jenkins自动化部署,需要用log4j2.yml文件
第一步,配置pom文件
1.先去除springboot自带的logging
2.添加log4j2依赖
3.添加yml识别
<!-- Spring Boot Web 依赖 -->
<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>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
第二步配置log4j.yml 文件
Configuration:
status: info
monitorInterval: 30
Properties:
property:
-
name: log_file_name
value: "databus.log"
Appenders:
RollingFile:
name: "serviceAppender"
fileName: "${log_file_name}"
filePattern: "${log_file_name}.%i"
PatternLayout:
Pattern: "%d %p %c{1.} [%t] %C{1} %M %m %ex%n"
Policies:
SizeBasedTriggeringPolicy:
size: "50 MB"
DefaultRolloverStrategy:
max: 20
Console:
name: "consoleAppender"
target: "SYSTEM_OUT"
PatternLayout:
Pattern: "%d %p %c{1.} [%t] %C{1} %M %m %ex%n"
Loggers:
Root:
level: debug
AppenderRef:
- ref: "serviceAppender"
- ref: "consoleAppender"
第三步application.yml 文件加载log4j2.yml(可有可无)
如果你的日志不区分生产开发测试不同的配置那么这步可以省略
如果你区分开发日志,测试日志,生产日志配置不同路径没那么要在application.yml文件配置加载log4j2.yml
如图区分:
application.yml文件添加下面分别加载不同log4j2.yml文件。
logging:
config: classpath:log4j2-development.yml
结束
感谢观看