最开始使用 SpringBoot 时,控制台日志是带彩色的,让人眼前一亮
后来彩色莫名丢失,由于影响不大,一直没有处理。
最近找到了解决方法(其实是因为自定义 logback.xml
):
spring:
output:
ansi:
enabled: always # 强制启用 ansi 输出
logging:
pattern:
# 控制台日志格式
console: "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(%-40.40logger{39}){cyan} : %msg%n"
生产环境配置了三种日志:控制台、分钟文件、每日文件
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<property name="LOG_PATH" value="${LOG_PATH:-logs}"/>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<logger name="org.springframework.web" level="INFO"/>
<springProfile name="prod">
<appender name="dailyRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_FILE}File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}.%i.logFileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30maxHistory>
rollingPolicy>
<encoder>
<Pattern>${FILE_LOG_PATTERN}Pattern>
encoder>
appender>
<appender name="minuteRollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_PATH}/minutes/%d{yyyyMMdd-HHmm}.logFileNamePattern>
<maxHistory>60maxHistory>
rollingPolicy>
<encoder>
<Pattern>${FILE_LOG_PATTERN}Pattern>
encoder>
appender>
<logger name="{项目包名}" level="INFO"/>
<logger name="org.springframework.data.mongodb.core.MongoTemplate" level="INFO"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="dailyRollingFileAppender"/>
<appender-ref ref="minuteRollingFileAppender"/>
root>
springProfile>
<springProfile name="dev,test">
<logger name="{项目包名}" level="DEBUG"/>
<logger name="org.springframework.data.mongodb.core.MongoTemplate" level="DEBUG"/>
<root level="INFO">
<appender-ref ref="CONSOLE" />
root>
springProfile>
configuration>