IDEA 设置 SpringBoot logback 彩色日志的解决方法 附配置文件
目录
- 1、背景说明
- 2、配置彩色
- 3、logback.XML
1、背景说明
最开始使用 SpringBoot 时,控制台日志是带彩色的,让人眼前一亮
后来彩色莫名丢失,由于影响不大,一直没有处理。2、配置彩色
最近找到了解决方法(其实是因为自定义 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"
3、logback.xml
生产环境配置了三种日志:控制台、分钟文件、每日文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!--不显示 Logback 默认的启动信息--> <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="${kchlxGYoCCLOG_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.corehttp://www.devze.com.rolling.RollingFileAppender"> <File>${LOG_FILE}</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicyandroid"> <!-- daily rollover --> <FileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}.%i.log</FileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 50MB --> <maxFileSize>5MB</maxFileSize> </timeBasedFhttp://www.devze.comileNamingAndTriggeringPolicy> <!-- keep 30 days' worth of history --> <maxHistory>30</maxHistory> </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"> <!-- rollover every minute --> <FileNamePattern>${LOG_PATH}/minutes/%d{yyyyMMdd-HHmm}.log</FileNamePattern> <!-- keep 30 minutes' worth of history --> <maxHistory>60</maxHistory> </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="COpythonNSOLE" /> <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>
到此这篇关于IDEA 设置 SpringBoot logback 彩色日志(附配置文件)的文章就介绍到这了,更多相关SpringBoot logback 彩色日志内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论