您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关logback如何配置中变量和include,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
logback配置在实际应用中往往会遇到appender的配置中存在大量的重复内容,但是logback又不像编程语言一样支持继承,采用include和变量结合可以一定程度上解决这个问题。
<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则可以一定程度上解决这个问题。
把不同的内容用变量定义:
<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>
<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>
下面的这个配置文件的意思是定义了两个appender,这两个appender中名为ASYNC-LOG的appender是将com.xx.cloud.trail.client.async路径下面产生的级别为error的日志进行收集,保存在定义的文件夹下面,另一个appender同理,如果这个logback.xml文件想在其他的项目中被引用那么就需要用在iucluded标签将定义的appender跟logger包围起来,表示这个是可以被导入其他项目的
<?xml version="1.0" encoding="UTF-8"?> <included> <appender name="ASYNC_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件输出的文件名--> <FileNamePattern>/var/log/xx/cloud/trail/async/error.log.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <!-- 日志输出编码 --> <layout class="ch.qos.logback.classic.PatternLayout"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n </pattern> </layout> </encoder> <!--日志文件最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <appender name="AOP_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件输出的文件名--> <FileNamePattern>/var/log/xx/cloud/trail/aop/error.log.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <!-- 日志输出编码 --> <layout class="ch.qos.logback.classic.PatternLayout"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n </pattern> </layout> </encoder> <!--日志文件最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!--需要将日志单独输出的包路径--> <logger name="com.xx.cloud.trail.client.async" level="ERROR" additivity="false"> <appender-ref ref="ASYNC_LOG"/> </logger> <!--需要将日志单独输出的包路径--> <logger name="com.xx.cloud.trail.client.aop" level="ERROR" additivity="false"> <appender-ref ref="AOP_LOG"/> </logger> </included>
假如1中的配置logback.xml文件的名称为logback-trail.xml,那么需要在其他的项目的logback.xml文件中加入一句
表示导入其他项目的logback文件,这样就能将其他项目中的配置文件进行导入
<?xml version="1.0" encoding="UTF-8"?> <!-- scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 scanPeriod: 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 --> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <include resource="logback-trail.xml"/> <!--配置日志输出到的路径--> <springProperty scop="context" name="logging.path" source="logging.path" defaultValue="logging-path-not-find"/> <property name="log.path" value="${logging.path}"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <appender name="TRAIL_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件输出的文件名--> <FileNamePattern>${logging.path}/trail/trail.log.%d{yyyy-MM-dd}.log</FileNamePattern> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> <!-- 日志输出编码 --> <layout class="ch.qos.logback.classic.PatternLayout"> <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n </pattern> </layout> </encoder> <!--日志文件最大的大小--> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <!-- 日志输出级别 --> <root level="DEBUG"> <appender-ref ref="STDOUT"/> <appender-ref ref="TRAIL_LOG"/> </root> </configuration>
关于“logback如何配置中变量和include”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。