1.在pom.xmlz中移除springboot中对logback依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions> <!--移除框架中的logback依赖-->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
2.引入log4j
<!--log4j2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
3.在application.properties文件中配置日志的配置文件,此处配置的log4j2.xml为log4j的配置想xml文件,该文件与application.properties在同一路径下
#log4j2输出日志
logging.config=classpath:log4j2.xml
4.log4j2.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<!-- 控制台输出 -->
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class %L %M - %msg%n"/>
</console>
<!-- fileName:输出路径 filePattern:命名规则 -->
<RollingFile name="AllFile" fileName="logs/All.log"
filePattern="logs/$${date:yyyy-MM-dd}/allOut-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="all" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<!-- 输出格式 -->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%n" charset="UTF-8"/>
<Policies>
<!-- 每天生成一个日志文件 -->
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
<DefaultRolloverStrategy max="50"/>
</RollingFile>
<RollingFile name="ErrorFile" fileName="logs/Error.log"
filePattern="logs/$${date:yyyy-MM-dd}/err-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<!-- 输出格式 -->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" charset="UTF-8"/>
<Policies>
<!-- 每天生成一个日志文件 -->
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<!-- SizeBasedTriggeringPolicy单个文件的大小限制 -->
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<!-- DefaultRolloverStrategy同一个文件下的最大文件数 -->
<DefaultRolloverStrategy max="50"/>
</RollingFile>
</appenders>
<loggers>
<!--过滤掉spring无用的debug信息-->
<logger name="org.springframework" level="error"></logger>
<logger name="com.zhangxiaosan.top" level="debug"></logger>
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="AllFile"/>
<appender-ref ref="ErrorFile"/>
</root>
</loggers>
</configuration>