参考文章https://blog.csdn.net/fujaja/article/details/81530284
接着上篇文章,这次添加log4j2
1.修改pom.xml文件,要移除原来的logback,才能使用(这边坑了我好久)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
2.添加log4j2依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
3.log4j2.xml文件(没有深入研究,先用起来)
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!-- status log4j2内部日志级别 -->
<configuration status="INFO">
<!-- 全局参数 -->
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %c{1}:%L -%m%n</Property>
<Property name="displayName">newsLog</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
</Console>
<!-- 文件 每次运行程序会自动清空,由append属性决定 -->
<File name="error" fileName="${displayName}_error.log" append="true">
<!-- 指定error 级别的日志 -->
<ThresholdFilter level="ERROR" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
</File>
<!-- 滚动文件 -->
<RollingFile name="rollingFile" fileName="${displayName}.log"
filePattern="${displayName}_%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<!-- 按大小划分 -->
<SizeBasedTriggeringPolicy size="50 MB" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="WARN" />
<Logger name="com.wy.news" level="DEBUG" />
<Root level="DEBUG">
<AppenderRef ref="console"></AppenderRef>
<AppenderRef ref="error"></AppenderRef>
<AppenderRef ref="rollingFile"></AppenderRef>
</Root>
</Loggers>
</configuration>
这个可以配置多个日志输出级别
4.修改application.yml文件
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/mytest?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
logging:
config:
classpath: log4j.xml
5.启动,刷新工程,日志文件就出来了