一份可以用于生产环境的Spring Boot日志配置文件

在springboot项目的resources下放置logback-spring.xml,内容为

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 引入默认日志配置 -->
    <include resource="base.xml"/>
</configuration>

其中base.xml文件内容为

<?xml version="1.0" encoding="UTF-8"?>
<included>

    <!-- 引入各种appender配置 -->
    <include resource="console-appender.xml"/>
    <include resource="collect-file-appender.xml"/>

    <!-- 定义各个SpringBoot环境的写入情况 -->
    <springProfile name="dev">
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
            <appender-ref ref="ASYNC_FILE"/>
        </root>
    </springProfile>

    <springProfile name="test,gray,prod">
        <root level="INFO">
            <appender-ref ref="ASYNC_FILE"/>
        </root>
    </springProfile>
</included>

其中console-appender.xml文件内容为

<?xml version="1.0" encoding="UTF-8"?>
<included>
    <!-- 控制台输出中为不同的日志级别着色,以提高可读性 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <!-- 将日志事件中的异常信息转换为带有空格格式的字符串 -->
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <!-- 将日志事件中的异常信息转换为带有扩展空格格式的字符串 -->
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>

    <!-- 定义属性,日志格式,如果想覆盖默认格式可以在配置文件中通过‘business.log.filePattern’来指定 -->
    <springProperty scope="context" name="CONSOLE_LOG_PATTERN" source="business.log.filePattern"
                    defaultValue="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint}|%clr(%level)|%0.15t|%clr(%-0.40logger{39}){cyan}%clr(|){magenta}%M%clr(|){magenta}%L%clr(|){magenta}req_id=%X{req_id},trace_id=%X{trace_id},remote_ip=%X{remote_ip},client_ip=%X{client_ip},uid=%X{uid}%clr(|){magenta} %msg%n"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
</included>

其中collect-file-appender.xml文件内容为

<?xml version="1.0" encoding="UTF-8"?>
<included>

    <!-- 定义变量,定义日志保存的路径 -->
    <define name="logPath" class="com.xxxxx.web.config.MyLogPathDefiner"/>

    <!-- 定义常用变量-->
    <springProperty scope="context" name="SERVICE_NAME" source="spring.application.name"/>
    <springProperty scope="context" name="SERVER_PORT" source="server.port" defaultValue="0"/>
    <springProperty scope="context" name="maxHistory" source="business.log.maxHistory" defaultValue="31"/>
    <springProperty scope="context" name="maxFileSize" source="business.log.maxFileSize" defaultValue="500MB"/>
    <springProperty scope="context" name="totalSizeCap" source="business.log.totalSizeCap" defaultValue="20GB"/>
    <springProperty scope="context" name="discardingThreshold" source="business.log.discardingThreshold" defaultValue="0"/>
    <springProperty scope="context" name="queueSize" source="business.log.queueSize" defaultValue="1024"/>
    <springProperty scope="context" name="neverBlock" source="business.log.queueSize" defaultValue="true"/>
    <springProperty scope="context" name="FILE_PATTERN" source="business.log.filePattern"
                    defaultValue="%d{yyyy-MM-dd HH:mm:ss.SSS}|%level|%0.15t|%-0.40logger{39}|req_id=%X{req_id},trace_id=%X{trace_id},remote_ip=%X{remote_ip},client_ip=%X{client_ip},uid=%X{uid}| %msg%n"/>

    <!-- 定义普通文件滚动压缩文件写入器 -->
    <appender name="SPRING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logPath}/spring.${SERVER_PORT}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/spring.${SERVER_PORT}.%d{yyyy_MM_dd}.%i.log.gz</fileNamePattern>
            <maxHistory>${maxHistory}</maxHistory>
            <maxFileSize>${maxFileSize}</maxFileSize>
            <totalSizeCap>${totalSizeCap}</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- 定义异步日志记录器 -->
    <appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
        <neverBlock>${neverBlock}</neverBlock>
        <discardingThreshold>${discardingThreshold}</discardingThreshold>
        <queueSize>${queueSize}</queueSize>
        <appender-ref ref="SPRING_FILE"/>
    </appender>

</included>

其中MyLogPathDefiner文件内容为

public class MyLogPathDefiner extends PropertyDefinerBase {
    @Override
    public String getPropertyValue() {
        return "/data/log";
    }
}

注意:如果在日志中想额外打印一些参数,可以通过内置函数来定义

MDC.put("key1", "val1");

这样就可以直接在日志文件中直接使用key1这个变量了。

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值