相当美观实用的日志配置(SpringBoot)

该配置展示了如何在SpringBoot应用中设置日志系统,包括使用logback进行详细配置,定义不同级别(如TRACE,INFO,DEBUG,ERROR)的日志文件,设置时间及大小触发的日志滚动策略,以及控制台和文件输出的格式。此外,还特别设置了颜色编码以增强日志可读性,并针对特定包或类进行了调试级别配置。
摘要由CSDN通过智能技术生成

相当美观实用的日志配置(SpringBoot)

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="1000 seconds">
    <!-- 将状态信息监听器设置为无操作监听器。-->
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
    <springProperty scope="context" name="spring.application.name" source="spring.application.name" defaultValue="app"/>
    <timestamp key="date" datePattern="yyyyMMdd"/>
    <property name="colorfulPattern"
              value="%blue(%d{MM-dd HH:mm:ss}) [%boldYellow(%thread)] | %highlight(%-5level) | %boldGreen(%logger) %X{clientIp} %X{operateId} - %highlight(%msg) %n"/>
    <property name="defaultPattern"
              value="%d{MM-dd HH:mm:ss} [%thread] | %-5level | %logger %X{clientIp} %X{operateId} - %msg %n"/>
    <property name="log.base_path" value="logs"/>
    <!-- 日志文件大小,超过这个大小将被压缩 -->
    <property name="log.max.size" value="100MB"/>

    <!--控制台输出 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${colorfulPattern}</pattern>
            <!--            <pattern>${defaultPattern}</pattern>-->
        </encoder>
    </appender>

    <appender name="trace.appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.base_path}/${spring.application.name}/trace.${spring.application.name}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>
                ${log.base_path}/archive/${spring.application.name}/trace.${spring.application.name}.%d{yyyyMMdd}.%i.log.zip
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${defaultPattern}</pattern>
        </layout>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>TRACE</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="info.appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.base_path}/${spring.application.name}/info.${spring.application.name}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>
                ${log.base_path}/archive/${spring.application.name}/info.${spring.application.name}.%d{yyyyMMdd}.%i.log.zip
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${defaultPattern}</pattern>
        </layout>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="debug.appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.base_path}/${spring.application.name}/debug.${spring.application.name}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>
                ${log.base_path}/archive/${spring.application.name}/debug.${spring.application.name}.%d{yyyyMMdd}.%i.log.zip
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${defaultPattern}</pattern>
        </layout>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <appender name="error.appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log.base_path}/${spring.application.name}/error.${spring.application.name}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>
                ${log.base_path}/archive/${spring.application.name}/error.${spring.application.name}.%d{yyyyMMdd}.%i.log.zip
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${defaultPattern}</pattern>
        </layout>
        <!-- 下面为配置只输出error级别的日志 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>
    <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
    <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
    <!-- 添加附加的appender,最多只能添加一个 -->
    <appender name="trace.async" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>256</queueSize>
        <includeCallerData>true</includeCallerData>
        <appender-ref ref="trace.appender"/>
    </appender>
    <!-- http rest 请求日志 -->
    <logger name="cn.felord.common.http.RestBodyAdvice" level="TRACE"/>
    <logger name="cn.felord.app.mapper" level="DEBUG"/>
    <!--    dev 环境调试-->
    <springProfile name="dev">
        <logger name="org.springframework.security" level="TRACE" additivity="false">
            <appender-ref ref="stdout"/>
        </logger>
        <logger name="org.springframework.security.oauth2" level="DEBUG" additivity="false">
            <appender-ref ref="stdout"/>
        </logger>
        <logger name="org.springframework.web" level="TRACE" additivity="false">
            <appender-ref ref="stdout"/>
        </logger>
    </springProfile>

    <root level="info">
        <appender-ref ref="stdout"/>
        <appender-ref ref="info.appender"/>
        <appender-ref ref="debug.appender"/>
        <appender-ref ref="error.appender"/>
        <appender-ref ref="trace.async"/>
    </root>
</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值