第一步:修改pom文件
XML
<!-- Spring Boot Starter WebFlux (排除默认日志) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加Log4j2 Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>${spring-boot.version}</version>
</dependency>
第二不:pom添加关键依赖
XML
<!-- 添加Log4j2核心依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- 添加Log4j2版本属性 -->
<log4j2.version>2.17.2</log4j2.version>
<spring-boot.version>2.7.18</spring-boot.version>
第三步:在resources文件夹下面新建log4j2.xml文件
XML
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30" name="log4j2">
<Properties>
<!-- 日志保存路径 -->
<Property name="LOG_PATH">logs</Property>
<!-- 美化后的控制台日志模式 - 彩色日志 -->
<Property name="CONSOLE_LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level} [%-20.20t] %style{%logger{36}}{cyan} : %msg%n</Property>
<!-- 美化后的文件日志模式 -->
<Property name="FILE_LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%-20.20t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<!-- 彩色控制台输出 -->
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}" disableAnsi="false"/>
</Console>
<!-- DEBUG级别文件输出 -->
<RollingFile name="DEBUG_FILE" fileName="${LOG_PATH}/log_debug.log"
filePattern="${LOG_PATH}/debug/log-debug-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${FILE_LOG_PATTERN}"/>
<Filters>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- INFO级别文件输出 -->
<RollingFile name="INFO_FILE" fileName="${LOG_PATH}/log_info.log"
filePattern="${LOG_PATH}/info/log-info-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${FILE_LOG_PATTERN}"/>
<Filters>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- WARN级别文件输出 -->
<RollingFile name="WARN_FILE" fileName="${LOG_PATH}/log_warn.log"
filePattern="${LOG_PATH}/warn/log-warn-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${FILE_LOG_PATTERN}"/>
<Filters>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- ERROR级别文件输出 -->
<RollingFile name="ERROR_FILE" fileName="${LOG_PATH}/log_error.log"
filePattern="${LOG_PATH}/error/log-error-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${FILE_LOG_PATTERN}"/>
<Filters>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
<!-- 异步Appender提高性能 -->
<Async name="ASYNC_DEBUG" bufferSize="1024">
<AppenderRef ref="DEBUG_FILE"/>
</Async>
<Async name="ASYNC_INFO" bufferSize="1024">
<AppenderRef ref="INFO_FILE"/>
</Async>
<Async name="ASYNC_WARN" bufferSize="1024">
<AppenderRef ref="WARN_FILE"/>
</Async>
<Async name="ASYNC_ERROR" bufferSize="1024">
<AppenderRef ref="ERROR_FILE"/>
</Async>
</Appenders>
<Loggers>
<!-- 项目特定包日志级别 -->
<Logger name="com.jdlt" level="debug" additivity="false">
<AppenderRef ref="CONSOLE"/>
<AppenderRef ref="ASYNC_DEBUG"/>
<AppenderRef ref="ASYNC_INFO"/>
<AppenderRef ref="ASYNC_WARN"/>
<AppenderRef ref="ASYNC_ERROR"/>
</Logger>
<!-- 减少Spring框架的日志输出 -->
<Logger name="org.springframework" level="info"/>
<Logger name="org.apache" level="info"/>
<Logger name="reactor" level="info"/>
<Root level="info">
<AppenderRef ref="CONSOLE"/>
<AppenderRef ref="ASYNC_DEBUG"/>
<AppenderRef ref="ASYNC_INFO"/>
<AppenderRef ref="ASYNC_WARN"/>
<AppenderRef ref="ASYNC_ERROR"/>
</Root>
</Loggers>
</Configuration>