SpringBoot项目默认使用logback, 已经内置了 logback 的相关jar包,会从resource包下查找logback.xml,
logback 文件格式范本 可直接复制使用,有控制台 info.log error.log三个部分
<!--可定期扫描配置文件的更改,并在更改时自动应用。默认每分钟一次-->
<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个字符宽度,%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>
<!-- 日志文件保留天数 -->
<maxHistory>60</maxHistory>
<!-- 日志文件最大大小:80MB -->
<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">
<!-- 日志输出级别,优先级 > '<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>
<!-- 默认日志输出级别 -->
<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