一 :根节点 <configuration></configuration>
1 <?xml version="1.0" encoding="utf-8"?> 2 1 <configuration debug="true" scan="true" scanPeriod="2"> 3 2 <!--TODO : 子节点信息--> 4 3 </configuration>
可以这样描述配置文件的基本结构:以<configuration>开头,后面有零
个或多个<appender>元素,有零个或多个<logger>元素,有最多一个<root>元素
二:<configuration> 子节点
<?xml version="1.0" encoding="utf-8"?> <configuration debug="true" scan="true" scanPeriod="2"> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> </appender> <!-- conf file out --> <appender name="file_out" class="ch.qos.logback.core.FileAppender"> </appender> <!-- conf file out --> <appender name="file_out" class="ch.qos.logback.core.RollingFileAppender"> </appender> <root></root> <loger></loger> </configuration>
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date [%thread] %-5level %logger - %message%newline</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="console_out" /> </root> </configuration>
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date [%thread] %-5level %logger - %message%newline</pattern> </encoder> </appender> <!-- conf file out --> <appender name="file_out" class="ch.qos.logback.core.FileAppender"> <file>logs/debug.log</file> <encoder> <pattern>%date [%thread] %-5level %logger - %message%newline</pattern> </encoder> </appender> </configuration>
- 1.1.3.1 :
<!-- 03:conf errorAppender out --> <appender name="errorAppender" class="ch.qos.logback.core.RollingFileAppender"> <file>logs/error.log</file> <!-- 设置滚动策略 --> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--设置日志命名模式--> <fileNamePattern>errorFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!--最多保留30天log--> <maxHistory>30</maxHistory> </rollingPolicy> <!-- 超过150MB时,触发滚动策略 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>150</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> </encoder> </appender>
- 1.1.3.2 :
2.1 : logger 节点
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 过滤掉非INFO级别 --> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- conf infoAppender out --> <appender name="infoAppender" class="ch.qos.logback.core.RollingFileAppender"> <file>logs/info.log</file> <!-- 设置滚动策略 --> <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--设置日志命名模式--> <fileNamePattern>infoFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!--最多保留30天log--> <maxHistory>30</maxHistory> </rollingPoliy> <!-- 超过150MB时,触发滚动策略 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>150</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> </encoder> </appender> <!-- 指定在logback.olf.log包中的log --> <logger name="logback.olf.log" level="info"> <appender-ref ref = "console_out"/> <appender-ref ref = "infoAppender"/> </logger> </configuration>
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 过滤掉非INFO级别 --> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 01:conf infoAppender out --> <appender name="infoAppender" class="ch.qos.logback.core.RollingFileAppender"> <file>logs/info.log</file> <!-- 设置滚动策略 --> <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--设置日志命名模式--> <fileNamePattern>infoFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!--最多保留30天log--> <maxHistory>30</maxHistory> </rollingPoliy> <!-- 超过150MB时,触发滚动策略 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>150</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> </encoder> </appender> <!-- 02:conf debugAppender out --> <appender name="debugAppender" class="ch.qos.logback.core.RollingFileAppender"> <file>logs/debug.log</file> <!-- 设置滚动策略 --> <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--设置日志命名模式--> <fileNamePattern>debugFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!--最多保留30天log--> <maxHistory>30</maxHistory> </rollingPoliy> <!-- 超过150MB时,触发滚动策略 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>150</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> </encoder> </appender> <!-- 03:conf errorAppender out --> <appender name="errorAppender" class="ch.qos.logback.core.RollingFileAppender"> <file>logs/error.log</file> <!-- 设置滚动策略 --> <rollingPoliy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--设置日志命名模式--> <fileNamePattern>errorFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!--最多保留30天log--> <maxHistory>30</maxHistory> </rollingPoliy> <!-- 超过150MB时,触发滚动策略 --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>150</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d [%p] %-5level %logger - %msg%newline</pattern> </encoder> </appender> <root level="ALL"> <appender-ref ref="infoAppender"/> <appender-ref ref="debugAppender"/> <appender-ref ref="errorAppender"/> </root> </configuration>
三 : <filter> 过滤节点
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 过滤掉非INFO级别 --> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder> <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="console_out" /> </root> </configuration>
3.2 : 临界值过滤器(ThresholdFilter)
ThresholdFilter 过滤掉低于指定临界值的事件 . 当记录的级别等于或高于临界值时 , ThresholdFilter 的decide()方法会返回NEUTRAL ; 当记录级别低于临界值时 , 事件会被拒绝 下面是个配置文件例子 :
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <!-- 过滤掉TRACE和DEBUG级别的日志 --> <level>INFO</level> </filter> <encoder> <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="console_out" /> </root> </configuration>
3.3 : 求值过滤器(EvaluatorFilter)
EvaluatorFilter 封装了 EventEvaluator(ch.qos.logback.core.boolex.EventEvaluator) , 评估 是否符合指定的条件
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.EvaluatorFilter"> <evaluator> <!--过滤掉所有日志中不包含hello字符的日志--> <expression> message.contains("hello") </expression> <onMatch>NEUTRAL</onMatch> <onMismatch>DENY</onMismatch> </evaluator> </filter> <encoder> <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="console_out" /> </root> </configuration>
3.4 : 匹配器(Matchers)
尽管能通过调用 String 类的 matches()方法进行模式匹配,但这会导致每次调用过滤器 时都会创建一个全新的 Pattern 对象。为消除这种开销,你可以预先定义一个或多个 Matcher 对象。一旦定义 matcher 后,就可以在求值表达式里重复引用它。
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- conf consoel out --> <appender name ="console_out" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.EvaluatorFilter"> <evaluator> <matcher> <Name>odd</Name> <!-- 过滤掉序号为奇数的语句--> <regex>statement [13579]</regex> </matcher> <expression>odd.matches(formattedMessage)</expression> <onMatch>NEUTRAL</onMatch> <onMismatch>DENY</onMismatch> </evaluator> </filter> <encoder> <pattern>%-4relative [%thread] %-5level %logger{30} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="console_out" /> </root> </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 配置示例:
MaxHistory指的是文件数量,超过MaxHistory数量才会删除,只有当每天生成且只生成一个文件时才表示保留天数
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration debug="false"> 3 <!--设置存储路径变量--> 4 <property name="LOG_HOME" value="./log"/> 5 6 <!--控制台输出appender--> 7 <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> 8 <!--设置输出格式--> 9 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 10 <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> 11 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> 12 <!--设置编码--> 13 <charset>UTF-8</charset> 14 </encoder> 15 </appender> 16 17 <!--文件输出,时间窗口滚动--> 18 <appender name="timeFileOutput" class="ch.qos.logback.core.rolling.RollingFileAppender"> 19 <!--日志名,指定最新的文件名,其他文件名使用FileNamePattern --> 20 <File>${LOG_HOME}/tpi.log</File> 21 <!--文件滚动模式--> 22 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 23 <!--日志文件输出的文件名,可设置文件类型为gz,开启文件压缩--> 24 <FileNamePattern>${LOG_HOME}/tpi.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern> 25 <!--日志文件保留天数--> 26 <MaxHistory>1</MaxHistory> 27 <!--按大小分割同一天的--> 28 <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 29 <maxFileSize>1GB</maxFileSize> 30 </timeBasedFileNamingAndTriggeringPolicy> 31 <!--每次启动删除多余日志--> 32 <cleanHistoryOnStart>true</cleanHistoryOnStart> 33 <totalSizeCap>2GB</totalSizeCap> 34 </rollingPolicy> 35 36 <filter class="ch.qos.logback.classic.filter.LevelFilter"> 37 <level>ERROR</level> <!--过滤掉error的级别--> 38 <onMatch>DENY</onMatch> 39 <onMismatch>ACCEPT</onMismatch> 40 </filter> 41 42 <!--输出格式--> 43 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 44 <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> 45 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> 46 <!--设置编码--> 47 <charset>UTF-8</charset> 48 </encoder> 49 50 </appender> 51 52 <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> 53 <file>${LOG_HOME}/error.log</file> 54 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 55 <fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern> 56 <MaxHistory>1</MaxHistory> 57 <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 58 <maxFileSize>500MB</maxFileSize> 59 </timeBasedFileNamingAndTriggeringPolicy> 60 <cleanHistoryOnStart>true</cleanHistoryOnStart> 61 <totalSizeCap>2GB</totalSizeCap> 62 </rollingPolicy> 63 <!-- 过滤日志 --> 64 <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> 65 <level>ERROR</level> 66 </filter> 67 <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> 68 <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}[%L] - %msg%n</pattern> 69 <charset>UTF-8</charset> 70 </encoder> 71 </appender> 72 73 <!--指定基础的日志输出级别--> 74 <root level="INFO"> 75 <!--appender将会添加到这个loger--> 76 <appender-ref ref="console"/> 77 <appender-ref ref="timeFileOutput"/> 78 <appender-ref ref="FILE_ERROR" /> 79 </root> 80 </configuration>