IDEA 设置 SpringBoot logback 彩色日志(附配置文件)

发布时间:2023年12月21日

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="${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">
                <!-- 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>
                </timeBasedFileNamingAndTriggeringPolicy>

                <!-- 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="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>
文章来源:https://blog.csdn.net/ssrc0604hx/article/details/135099632
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。