Logback配置中变量和Include的应用方式

logback配置在实际应用中往往会遇到appender的配置中存在大量的重复内容,但是logback又不像编程语言一样支持继承,采用include和变量结合可以一定程度上解决这个问题。

Example:

<appender class="ch.qos.logback.core.rolling.RollingFileAppender" name=WarnLogFile">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
        </encoder>
        <file>${LOGS_DIR}/warn.log</file>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>40MB</maxFileSize>
        </triggeringPolicy>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGS_DIR}/warn%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>21</maxIndex>
        </rollingPolicy>
    </appender>
    <appender class="ch.qos.logback.classic.AsyncAppender" name="Warn">
        <queueSize>2048</queueSize>
        <includeCallerData>true</includeCallerData>
        <discardingThreshold>0</discardingThreshold>
        <appender-ref ref="WarnLogFile" />
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level>
        </filter>
    </appender>

    <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="InfoLogFile">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
        </encoder>
        <file>${LOGS_DIR}/info.log</file>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGS_DIR}/info%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>21</maxIndex>
        </rollingPolicy>
    </appender>
    <appender class="ch.qos.logback.classic.AsyncAppender" name="info">
        <queueSize>2048</queueSize>
        <includeCallerData>true</includeCallerData>
        <discardingThreshold>0</discardingThreshold>
        <appender-ref ref="infoLogFile" />
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </appender>

    <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="DebugLogFile">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
        </encoder>
        <file>${LOGS_DIR}/debug.log</file>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>5MB</maxFileSize>
        </triggeringPolicy>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGS_DIR}/debug%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>21</maxIndex>
        </rollingPolicy>
    </appender>
    <appender class="ch.qos.logback.classic.AsyncAppender" name="Debug">
        <queueSize>2048</queueSize>
        <includeCallerData>true</includeCallerData>
        <discardingThreshold>0</discardingThreshold>
        <appender-ref ref="DebugLogFile" />
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
    </appender>

可以看到以上的三个定义存在大量的重复内容会导致logback文件很大,不容易维护,结合变量和include则可以一定程度上解决这个问题。

首先把配置内容抽取一个公共文件default.xml,把不同的内容用变量定义:

<included>
    <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="${FILE_APPENDER}">
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{dd.MMM.yyyy HH:mm:ss.SSS z}, [%6t], %6p, %C:%M %m%n</pattern>
        </encoder>
        <file>${LOGS_DIR}/${LOGFILE}.log</file>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>5MB</maxFileSize>
        </triggeringPolicy>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOGS_DIR}/${LOGFILE}%i.log.gz</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>21</maxIndex>
        </rollingPolicy>
    </appender>
    <appender class="ch.qos.logback.classic.AsyncAppender" name="${ASYNC_APPENDER}">
        <queueSize>2048</queueSize>
        <includeCallerData>true</includeCallerData>
        <discardingThreshold>0</discardingThreshold>
        <appender-ref ref="${FILE_APPENDER}" />
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${LOG_LEVEL}</level>
        </filter>
    </appender>
</included>

然后基于公共文件定义各自的配置

warn.xml:

<included>
    <property scope="local" name="LOGFILE" value="warn" />
    <property scope="local" name="FILE_APPENDER" value="WarnLogFile" />
    <property scope="local" name="ASYNC_APPENDER" value="warn" />
    <property scope="local" name="LOG_LEVEL" value="WARN" />
    
    <include file="${LOGS_CUSTOM_DIR}/default.xml"/>
</included>

info.xml:

<included>
    <property scope="local" name="LOGFILE" value="info" />
    <property scope="local" name="FILE_APPENDER" value="InfoLogFile" />
    <property scope="local" name="ASYNC_APPENDER" value="info" />
    <property scope="local" name="LOG_LEVEL" value="INFO" />
    
    <include file="${LOGS_CUSTOM_DIR}/default.xml"/>
</included>

debug.xml:

<included>
    <property scope="local" name="LOGFILE" value="debug" />
    <property scope="local" name="FILE_APPENDER" value="DebugLogFile" />
    <property scope="local" name="ASYNC_APPENDER" value="debug" />
    <property scope="local" name="LOG_LEVEL" value="DEBUG" />
    
    <include file="${LOGS_CUSTOM_DIR}/default.xml"/>
</included>

最后在logback.xml中就可以include上面定义的文件:

<include file="${LOGS_CUSTOM_DIR}/warn.xml"/>
    <include file="${LOGS_CUSTOM_DIR}/info.xml"/>
    <include file="${LOGS_CUSTOM_DIR}/debug.xml"/>

    <root level="DEBUG">
        ...
        <appender-ref ref="warn"/>
        <appender-ref ref="info"/>
        <appender-ref ref="debug"/>
    </root>
收藏 (0)
评论列表
正在载入评论列表...
我是有底线的
为您推荐
    暂时没有数据