完美spring boot 使用log4j2按級別輸出到不同檔案
阿新 • • 發佈:2019-02-04
1. pom.xml引入需要的jar
<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>
<!--log4j2 日誌框架-->
<dependency>
<groupId >org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional >true</optional>
</dependency>
2. 編寫log4j2配置檔案(注意看註釋)
<?xml version="1.0" encoding="UTF-8"?>
<!--日誌級別以及優先順序排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration後面的status,這個用於設定log4j2自身內部的資訊輸出,可以不設定,當設定成trace時,你會看到log4j2內部各種詳細輸出-->
<!--monitorInterval:Log4j能夠自動檢測修改配置 檔案和重新配置本身,設定間隔秒數-->
<Configuration status="WARN" monitorInterval="30">
<!--全域性屬性-->
<Properties>
<Property name="APP_NAME">luis-log4j2</Property>
<Property name="LOG_FILE_PATH">${sys:user.home}/logs/${APP_NAME}</Property>
<Property name="PATTERN_FORMAT">%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n</Property>
</Properties>
<Appenders>
<!--輸出到控制檯-->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN_FORMAT}"/>
</Console>
<!--輸出到檔案 用來定義超過指定大小自動刪除舊的建立新的的Appender.-->
<RollingFile name="RollingInfoFile" fileName="${LOG_FILE_PATH}/info.log"
filePattern="${LOG_FILE_PATH}/$${date:yyyyMM}/info-%d{yyyyMMdd}-%i.log.gz">
<!--控制檯只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern>${PATTERN_FORMAT}</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches 10 MegaBytes -->
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
<!--輸出警告日誌到檔案-->
<RollingFile name="RollingWarnFile" fileName="${LOG_FILE_PATH}/warn.log"
filePattern="${LOG_FILE_PATH}/$${date:yyyyMM}/warn-%d{yyyyMMdd}-%i.log.gz">
<!--控制檯只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout>
<pattern>${PATTERN_FORMAT}</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches 10 MegaBytes -->
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
<!--輸出錯誤日誌到檔案-->
<RollingFile name="RollingErrorFile" fileName="${LOG_FILE_PATH}/error.log"
filePattern="${LOG_FILE_PATH}/$${date:yyyyMM}/error-%d{yyyyMMdd}-%i.log.gz">
<!--控制檯只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch)-->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>${PATTERN_FORMAT}</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches 10 MegaBytes -->
<OnStartupTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100 MB"/>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!--過濾掉spring和mybatis的一些無用的DEBUG資訊-->
<Logger name="org.springframework" level="INFO"/>
<Logger name="org.mybatis" level="INFO"/>
<!-- LOG "com.luis*" at TRACE level -->
<Logger name="com.luis" level="INFO"/>
<!-- LOG everything at INFO level -->
<Root level="ALL">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingInfoFile"/>
<AppenderRef ref="RollingWarnFile"/>
<AppenderRef ref="RollingErrorFile"/>
</Root>
</Loggers>
</Configuration>
注意:按日誌級別輸出到不同的檔案,需要使用Filters,並且先拒絕不符合的,在接受符合的順序;
3. 編寫測試類
@Log4j2
@RestController
public class LombokLogController {
@GetMapping("lombokLog")
public String lombokLog(){
log.trace("lombok info log!");
log.debug("lombok debug log!");
log.info("lombok info log!");
log.warn("lombok warn log!");
log.error("lombok error log!");
log.fatal("lombok fatal log!");
return "use lombok log!";
}
}
4. 檢視結果