1厘灼、添加pom依賴
<!--全局排除spring-boot-starter-logging內(nèi)的所有依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j2依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
2副编、在src/main/resources目錄下增加log4j2的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="info">
<properties>
<!-- 日志存放根目錄 -->
<property name="LOG_HOME">D:/workspace-summer/logs</property>
<!-- 應(yīng)用名稱 -->
<property name="APP_NAME">msp-gateway</property>
<!-- 正常日志文件名稱 -->
<property name="FILE_NAME">${APP_NAME}-normal</property>
<!-- 錯誤日志文件名稱 -->
<property name="ERROR_FILE_NAME">${APP_NAME}-error</property>
<!-- 日志打印格式 -->
<property name="PATTERN">%d|%-5level|%-15t|%X{channel_serial_no}|%X{serial_no}|%X{trans_code}|[%-40c:%-3L]|%m%n
</property>
<!-- 單個文件最大大小,單位GB、MB养叛、KB等崔慧,詳見log4j2官網(wǎng) -->
<property name="SINGLE_FILE_MAX_SIZE">100MB</property>
<!-- 單個日期目錄中最大保留文件個數(shù) -->
<property name="SINGLE_DAY_FILE_MAX_COUNT">100</property>
</properties>
<appenders>
<!-- 控制臺追加器 -->
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="TRACE" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout pattern="${PATTERN}" />
</Console>
<!-- 文本追加器:文件滾動 -->
<RollingFile name="ROLLING_FILE"
fileName="${LOG_HOME}/${FILE_NAME}.log"
filePattern="${LOG_HOME}/$${date:yyyy-MM-dd}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${PATTERN}" />
<Policies>
<TimeBasedTriggeringPolicy modulate="true"
interval="1" />
<!-- <CronTriggeringPolicy schedule="${schedule}"/> -->
<SizeBasedTriggeringPolicy
size="${SINGLE_FILE_MAX_SIZE}" />
</Policies>
<DefaultRolloverStrategy
max="${SINGLE_DAY_FILE_MAX_COUNT}">
</DefaultRolloverStrategy>
</RollingFile>
<!-- 錯誤日志 -->
<RollingFile name="ROLLING_FILE_ERROR"
fileName="${LOG_HOME}/${ERROR_FILE_NAME}.log"
filePattern="${LOG_HOME}/$${date:yyyy-MM-dd}/${ERROR_FILE_NAME}-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${PATTERN}" />
<Policies>
<TimeBasedTriggeringPolicy modulate="true"
interval="1" />
<!-- <CronTriggeringPolicy schedule="${schedule}"/> -->
<SizeBasedTriggeringPolicy
size="${SINGLE_FILE_MAX_SIZE}" />
</Policies>
<DefaultRolloverStrategy
max="${SINGLE_DAY_FILE_MAX_COUNT}">
</DefaultRolloverStrategy>
</RollingFile>
</appenders>
<loggers>
<root level="INFO">
<!-- 輸出到控制臺 -->
<appender-ref ref="CONSOLE" />
<appender-ref ref="ROLLING_FILE" />
<appender-ref ref="ROLLING_FILE_ERROR" level="ERROR" />
</root>
<!-- Spring日志只打印 -->
<logger name="org.springframework" level="INFO">
</logger>
<!-- 業(yè)務(wù)日志 -->
<asyncLogger name="com.tdou" level="INFO"
includeLocation="true" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="ROLLING_FILE" />
<appender-ref ref="ROLLING_FILE_ERROR" level="ERROR" />
</asyncLogger>
<!-- 數(shù)據(jù)庫操作日志 -->
<logger name="com.tdou.merchant.dao" level="TRACE"
additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="ROLLING_FILE" />
</logger>
</loggers>
</configuration>