logback节点配置详解
一 :根节点 <configuration></configuration>
<?xml version="1.0" encoding="utf-8"?>
1 <configuration debug="true" scan="true" scanPeriod="2"> 2 <!--TODO : 子节点信息--> 3 </configuration>
可以这样描述配置文件的基本结构:以<configuration>开头,后面有零
个或多个<appender>元素,有零个或多个<logger>元素,有最多一个<root>元素
二:<configuration> 子节点
<?xml version="1.0" encoding="utf-8"?>
1 <configuration debug="true" scan="true" scanPeriod="2"> 2 <!-- conf consoel out --> 3 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 4 </appender> 5 6 <!-- conf file out --> 7 <appender name="file_out" class="ch.qos.logback.core.FileAppender"> 8 </appender> 9 10 <!-- conf file out --> 11 <appender name="file_out" class="ch.qos.logback.core.RollingFileAppender"> 12 </appender> 13 14 <root></root> 15 <loger></loger> 16 </configuration>
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <encoder> 6 <pattern>%date [%thread] %-5level %logger - %message%newline</pattern> 7 </encoder> 8 </appender> 9 10 <root level="INFO"> 11 <appender-ref ref="console_out" /> 12 </root> 13 </configuration>
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <encoder> 6 <pattern>%date [%thread] %-5level %logger - %message%newline</pattern> 7 </encoder> 8 </appender> 9 10 <!-- conf file out --> 11 <appender name="file_out" class="ch.qos.logback.core.FileAppender"> 12 <file>logs/debug.log</file> 13 <encoder> 14 <pattern>%date [%thread] %-5level %logger - %message%newline</pattern> 15 </encoder> 16 </appender> 17 </configuration>
- 1.1.3.1 :
1 <!-- 03:conf errorAppender out --> 2 <appender name="errorAppender" class="ch.qos.logback.core.RollingFileAppender"> 3 <file>logs/error.log</file> 4 <!-- 设置滚动策略 --> 5 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 6 <!--设置日志命名模式--> 7 <fileNamePattern>errorFile.%d{yyyy-MM-dd}.log</fileNamePattern> 8 <!--最多保留30天log--> 9 <maxHistory>30</maxHistory> 10 </rollingPolicy> 11 <!-- 超过150MB时,触发滚动策略 --> 12 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 13 <maxFileSize>150</maxFileSize> 14 </triggeringPolicy> 15 <encoder> 16 <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> 17 </encoder> 18 </appender>
- 1.1.3.2 :
2.1 : logger 节点
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 6 <!-- 过滤掉非INFO级别 --> 7 <level>INFO</level> 8 <onMatch>ACCEPT</onMatch> 9 <onMismatch>DENY</onMismatch> 10 </filter> 11 </appender> 12 13 <!-- conf infoAppender out --> 14 <appender name="infoAppender" class="ch.qos.logback.core.RollingFileAppender"> 15 16 <file>logs/info.log</file> 17 <!-- 设置滚动策略 --> 18 <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 19 <!--设置日志命名模式--> 20 <fileNamePattern>infoFile.%d{yyyy-MM-dd}.log</fileNamePattern> 21 <!--最多保留30天log--> 22 <maxHistory>30</maxHistory> 23 </rollingPoliy> 24 <!-- 超过150MB时,触发滚动策略 --> 25 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 26 <maxFileSize>150</maxFileSize> 27 </triggeringPolicy> 28 <encoder> 29 <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> 30 </encoder> 31 </appender> 32 33 <!-- 指定在logback.olf.log包中的log --> 34 <logger name="logback.olf.log" level="info"> 35 <appender-ref ref = "console_out"/> 36 <appender-ref ref = "infoAppender"/> 37 </logger> 38 </configuration>
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 6 <!-- 过滤掉非INFO级别 --> 7 <level>INFO</level> 8 <onMatch>ACCEPT</onMatch> 9 <onMismatch>DENY</onMismatch> 10 </filter> 11 </appender> 12 13 <!-- 01:conf infoAppender out --> 14 <appender name="infoAppender" class="ch.qos.logback.core.RollingFileAppender"> 15 16 <file>logs/info.log</file> 17 <!-- 设置滚动策略 --> 18 <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 19 <!--设置日志命名模式--> 20 <fileNamePattern>infoFile.%d{yyyy-MM-dd}.log</fileNamePattern> 21 <!--最多保留30天log--> 22 <maxHistory>30</maxHistory> 23 </rollingPoliy> 24 <!-- 超过150MB时,触发滚动策略 --> 25 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 26 <maxFileSize>150</maxFileSize> 27 </triggeringPolicy> 28 <encoder> 29 <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> 30 </encoder> 31 </appender> 32 33 <!-- 02:conf debugAppender out --> 34 <appender name="debugAppender" class="ch.qos.logback.core.RollingFileAppender"> 35 <file>logs/debug.log</file> 36 <!-- 设置滚动策略 --> 37 <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 38 <!--设置日志命名模式--> 39 <fileNamePattern>debugFile.%d{yyyy-MM-dd}.log</fileNamePattern> 40 <!--最多保留30天log--> 41 <maxHistory>30</maxHistory> 42 </rollingPoliy> 43 <!-- 超过150MB时,触发滚动策略 --> 44 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 45 <maxFileSize>150</maxFileSize> 46 </triggeringPolicy> 47 <encoder> 48 <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> 49 </encoder> 50 </appender> 51 52 <!-- 03:conf errorAppender out --> 53 <appender name="errorAppender" class="ch.qos.logback.core.RollingFileAppender"> 54 <file>logs/error.log</file> 55 <!-- 设置滚动策略 --> 56 <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 57 <!--设置日志命名模式--> 58 <fileNamePattern>errorFile.%d{yyyy-MM-dd}.log</fileNamePattern> 59 <!--最多保留30天log--> 60 <maxHistory>30</maxHistory> 61 </rollingPoliy> 62 <!-- 超过150MB时,触发滚动策略 --> 63 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 64 <maxFileSize>150</maxFileSize> 65 </triggeringPolicy> 66 <encoder> 67 <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> 68 </encoder> 69 </appender> 70 71 <root level="ALL"> 72 <appender-ref ref="infoAppender"/> 73 <appender-ref ref="debugAppender"/> 74 <appender-ref ref="errorAppender"/> 75 </root> 76 </configuration>
三 : <filter> 过滤节点
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 6 <!-- 过滤掉非INFO级别 --> 7 <level>INFO</level> 8 <onMatch>ACCEPT</onMatch> 9 <onMismatch>DENY</onMismatch> 10 </filter> 11 12 <encoder> 13 <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> 14 </encoder> 15 </appender> 16 <root level="DEBUG"> 17 <appender-ref ref="console_out" /> 18 </root> 19 </configuration>
3.2 : 临界值过滤器(ThresholdFilter)
ThresholdFilter 过滤掉低于指定临界值的事件 . 当记录的级别等于或高于临界值时 , ThresholdFilter 的decide()方法会返回NEUTRAL ; 当记录级别低于临界值时 , 事件会被拒绝 下面是个配置文件例子 :
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<!-- 过滤掉TRACE和DEBUG级别的日志 --> 6 <level>INFO</level> 7 </filter> 8 9 <encoder> 10 <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> 11 </encoder> 12 </appender> 13 <root level="DEBUG"> 14 <appender-ref ref="console_out" /> 15 </root> 16 </configuration>
3.3 : 求值过滤器(EvaluatorFilter)
EvaluatorFilter 封装了 EventEvaluator(ch.qos.logback.core.boolex.EventEvaluator) , 评估 是否符合指定的条件
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <filter class="ch.qos.logback.classic.filter.EvaluatorFilter"> 6 <evaluator> 7 <!--过滤掉所有日志中不包含hello字符的日志--> 8 <expression> 9 message.contains("hello") 10 </expression> 11 <onMatch>NEUTRAL</onMatch> 12 <onMismatch>DENY</onMismatch> 13 </evaluator> 14 </filter> 15 16 <encoder> 17 <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> 18 </encoder> 19 </appender> 20 <root level="DEBUG"> 21 <appender-ref ref="console_out" /> 22 </root> 23 </configuration>
3.4 : 匹配器(Matchers)
尽管能通过调用 String 类的 matches()方法进行模式匹配,但这会导致每次调用过滤器 时都会创建一个全新的 Pattern 对象。为消除这种开销,你可以预先定义一个或多个 Matcher 对象。一旦定义 matcher 后,就可以在求值表达式里重复引用它。
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <!-- conf consoel out --> 4 <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> 5 <filter class="ch.qos.logback.classic.filter.EvaluatorFilter"> 6 <evaluator> 7 <matcher> 8 <Name>odd</Name> 9 <!-- 过滤掉序号为奇数的语句--> 10 <regex>statement [13579]</regex> 11 </matcher> 12 <expression>odd.matches(formattedMessage)</expression> 13 <onMatch>NEUTRAL</onMatch> 14 <onMismatch>DENY</onMismatch> 15 </evaluator> 16 </filter> 17 18 <encoder> 19 <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> 20 </encoder> 21 </appender> 22 <root level="DEBUG"> 23 <appender-ref ref="console_out" /> 24 </root> 25 </configuration>
logback 执行流程 :
1 : 获得过滤链的策略
依据过滤器链返回的结果做出不同的响应。共有三个响应结果:
FilterReply.DENY, 直接退出,不执行后续流程
FilterReply.NEUTRA,继续向下执行
FilterReply.ACCEPT,不进行步骤二,即类型输出类型检查
2 : 执行基本的选择规则
主要是比较下level,如果级别低直接退出后续执行
3 : 创建LoggingEvent对象
这个对象包裹一些基本信息,包括日志界别,信息本身,可能的异常信息,执行时间,执行线程,其实一些随日志请求一起发出的数据和MDC。其中MDC是用来装一些额外的上下文信息的。
4 : 调用appenders
此时logback会调用appender的doAppender,如果appender里有一些filer的话,此时也会调用
5 : 格式化输出结果
通常情况下都是由layout层将event格式化成String型。当然也有意外比如说SocketAppender就是将event格式化成流。
6 : .输出LoggingEvent
将格式化好的结果,输出到appender中记录的地址
注 : 标准logback .xml 配置 详见楼主另一篇博文
http://www.cnblogs.com/DeepLearing/p/5664941.html
可用于纪录多文件。
作者:刘彬
出处:http://www.cnblogs.com/Albin/
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面