SpringBoot項(xiàng)目默認(rèn)使用logback, 已經(jīng)內(nèi)置了 logback 的相關(guān)jar包,會從resource包下查找logback.xml,
logback 文件格式范本 可直接復(fù)制使用,有控制臺 info.log error.log三個(gè)部分
<!--可定期掃描配置文件的更改,并在更改時(shí)自動(dòng)應(yīng)用。默認(rèn)每分鐘一次-->
<configuration scan="true" scanPeriod="30 seconds">
<!--定義日志文件的存儲地址-->
<property name="LOGGING_PATH" value="./protal-log" />
<!-- 控制臺輸出 -->
<Appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%-5level:級別從左顯示5個(gè)字符寬度,%t表示線程名,%msg:日志消息,%n是換行符-->
<pattern>%green(%d{yyyy-MM-dd HH:mm:ss.SSS}) %blue(%-5level) ${PID:-} --- [%t] %yellow(%logger{50}) - %msg%n</pattern>
</encoder>
</appender>
<!-- info級別日志文件輸出 -->
<appender name="INFO_FILE_NAME" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 日志文件輸出的文件名 -->
<File>${LOGGING_PATH}/info.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 每日生成日志文件或日志文件大小超出限制后輸出的文件名模板 -->
<fileNamePattern>${LOGGING_PATH}/info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- 日志文件保留天數(shù) -->
<maxHistory>60</maxHistory>
<!-- 日志文件最大大?。?0MB -->
<maxFileSize>80MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level ${PID:-} --- [%t] %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- error級別日志文件輸出 -->
<appender name="ERROR_FILE_NAME" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 日志輸出級別,優(yōu)先級 > '<root level>' -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<File>${LOGGING_PATH}/error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOGGING_PATH}/error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
<maxFileSize>100MB</maxFileSize>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level ${PID:-} --- [%t] %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 默認(rèn)日志輸出級別 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="INFO_FILE_NAME" />
<appender-ref ref="ERROR_FILE_NAME" />
</root>
</configuration>
日志顏色樣式配置
%highlight","%black", "%red", "%green","%yellow","%blue", "%magenta","%cyan", "%white", "%gray", "%boldRed","%boldGreen", "%boldYellow", "%boldBlue", "%boldMagenta""%boldCyan", "%boldWhite