logback配置中变量和include的应用
程序员文章站
2022-03-05 08:12:41
...
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>
上一篇: 微信小程序开发中怎样实现数据交互
下一篇: Swap Nodes in Pairs