美文网首页
第七章:配置logback日志

第七章:配置logback日志

作者: 菜出意料 | 来源:发表于2023-12-06 14:13 被阅读0次

    配置Logback日志

    日志配置

    # 日志文件输出目录,在logback-spring.xml中引用
    log-home: /Volumes/k1/logs/${spring.application.name}
    logging:
      config: classpath:logback-spring.xml
    

    logback-spring.xml

    使用<springProperty>标签引用application.yaml或application.properties文件中的配置

    如:将配置文件中的log-home赋值给LOG_HOME变量

    <springProperty scope="context" name="LOG_HOME" source="log-home"/>
    
    <?xml version="1.0" encoding="utf-8"?>
    <configuration scan="true" scanPeriod="60 seconds" debug="false">
        <!-- 每个文件的最大限制 -->
        <property name="maxFileSize" value="100MB"/>
        <!-- 保留多少天的日志 -->
        <property name="maxHistory" value="30"/>
        <!-- 异步缓冲队列的深度,该值会影响性能.默认值为256 -->
        <property name="queueSize" value="256"/>
        <!-- 日志的总大小 -->
        <property name="totalSizeCap" value="20GB"/>
        <!-- 异步日志提取调用者数据标识 true/false -->
        <property name="includeCallerData" value="true"/>
        <property name="pattern-color" value="%green([%d{yyyy-MM-dd' 'HH:mm:ss.sss}]) [%C] [%t] [%L] %highlight(%-5level) %red(%logger{50}) - %yellow(%msg) %n"/>
        <contextName>aus-call-center</contextName>
        <springProperty scope="context" name="LOG_HOME" source="log-home"/>
        <appender name="CONSOLE-LOG" class="ch.qos.logback.core.ConsoleAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>debug</level>
            </filter>
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
                <pattern>${pattern-color}</pattern>
                <charset>UTF-8</charset>
            </encoder>
        </appender>
        <!--获取比info级别高(包括info级别)但除error级别的日志-->
        <appender name="INFO-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>ERROR</level>
                <onMatch>DENY</onMatch>
                <onMismatch>ACCEPT</onMismatch>
            </filter>
            <encoder>
                <pattern>[%d{yyyy-MM-dd' 'HH:mm:ss.sss}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
            </encoder>
            <file>${LOG_HOME}/info/console.log</file>
            <!--滚动策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--路径-->
                <fileNamePattern>${LOG_HOME}/info/auscallcenter_%d{yyyy-MM-dd}.log.gz</fileNamePattern>
                <maxHistory>${maxHistory}</maxHistory>
                <totalSizeCap>${totalSizeCap}</totalSizeCap>
            </rollingPolicy>
        </appender>
        <appender name="ERROR-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERROR</level>
            </filter>
            <encoder>
                <pattern>[%d{yyyy-MM-dd' 'HH:mm:ss.sss}] [%C] [%t] [%L] [%-5p] %m%n</pattern>
            </encoder>
            <file>${LOG_HOME}/error/error.log</file>
            <!--滚动策略-->
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <!--路径-->
                <fileNamePattern>${LOG_HOME}/error/auscallcenter_%d{yyyy-MM-dd}.log</fileNamePattern>
                <maxHistory>${maxHistory}</maxHistory>
                <totalSizeCap>${totalSizeCap}</totalSizeCap>
            </rollingPolicy>
        </appender>
    
        <!-- 异步输出 -->
        <appender name="ASYNC-INFO" class="ch.qos.logback.classic.AsyncAppender">
            <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
            <discardingThreshold>0</discardingThreshold>
            <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
            <queueSize>256</queueSize>
            <!-- 添加附加的appender,最多只能添加一个 -->
            <appender-ref ref="INFO-LOG"/>
        </appender>
    
        <appender name="ASYNC-ERROR" class="ch.qos.logback.classic.AsyncAppender">
            <!-- 不丢失日志.默认的,如果队列的80%已满,则会丢弃TRACT、DEBUG、INFO级别的日志 -->
            <discardingThreshold>0</discardingThreshold>
            <!-- 更改默认的队列的深度,该值会影响性能.默认值为256 -->
            <queueSize>256</queueSize>
            <!-- 添加附加的appender,最多只能添加一个 -->
            <appender-ref ref="ERROR-LOG"/>
        </appender>
    
        <!--<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
            <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
                    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level logger_name:%logger{36} - [%tid] - message:%msg%n</pattern>
                </layout>
            </encoder>
        </appender>-->
        <!-- skywalking grpc 日志收集 8.4.0版本开始支持 -->
        <!--<appender name="grpc-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
            <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
                <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
                    <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n</Pattern>
                </layout>
            </encoder>
        </appender>-->
        <root level="debug">
            <appender-ref ref="CONSOLE-LOG" />
        </root>
        <root level="info">
            <!--<appender-ref ref="console" />
            <appender-ref ref="grpc-log" />-->
            <appender-ref ref="ASYNC-INFO" />
            <appender-ref ref="ASYNC-ERROR" />
        </root>
    </configuration>
    
    

    相关文章

      网友评论

          本文标题:第七章:配置logback日志

          本文链接:https://www.haomeiwen.com/subject/mnejgdtx.html