logback 实现debug 日志和warn(error)日志分开打印
2016-06-15 17:42
435 查看
最近使用SLF4J 顺便迷上了logback, 想实现以前log4j那种 debug 和 warn(error) 日志分开打印到不同文件的方式,配置了半天没有成功,最后查找官方文档发现Filter可以用来实现我们想要的功能。
1 日志打印流程
![](http://img.blog.csdn.net/20160615173728537)
2 我们这里主要使用在Appender格式化message string 之前的filter来实现想要的功能。
Regular logback-classic filters extend the
Filters are organized as an ordered list and are based onternary logic. The
values, i.e. one of
then the logging event is processed normally.If the returned value is
In logback-classic, filters can be added to
other part of the logging event.
logback 提供了常用的LevelFilter, ThreadholdFilter.
accepts or denies the event, depending on theconfiguration of the
onMatch andonMismatch properties. Here is a sampleconfiguration file.
the threshold will be denied. Here is a sampleconfiguration file.
通过使用上面的两个过滤器,我们很容易得到想要的结果配置:
参考文章: http://logback.qos.ch/manual/filters.html
1 日志打印流程
2 我们这里主要使用在Appender格式化message string 之前的filter来实现想要的功能。
Regular logback-classic filters extend the
Filterabstract class which essentially consists of a single
decide()method taking an
ILoggingEventinstance as its parameter.
Filters are organized as an ordered list and are based onternary logic. The
decide(ILoggingEvent event)methodof each filter is called in sequence. This method returns one ofthe
FilterReplyenumeration
values, i.e. one of
DENY,
NEUTRALor
ACCEPT. If the valuereturned by
decide() is
DENY, then thelog event is dropped immediately without consulting the remainingfilters. If the value returned is
NEUTRAL, then thenext filter in the list is consulted. If there are no furtherfilters to consult,
then the logging event is processed normally.If the returned value is
ACCEPT, then the loggingevent is processed immediately skipping the invocation of theremaining filters.
In logback-classic, filters can be added to
Appenderinstances. By adding one or more filters to an appender, you can filter events by arbitrary criteria, such as the contents of the log message, the contents of the MDC, the time of day or any
other part of the logging event.
logback 提供了常用的LevelFilter, ThreadholdFilter.
LevelFilter
LevelFilterfilters events based on exact levelmatching. If the event's level is equal to the configured level,the filter
accepts or denies the event, depending on theconfiguration of the
onMatch andonMismatch properties. Here is a sampleconfiguration file.
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <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" /> </root> </configuration>
ThresholdFilter
TheThresholdFilterfilters events below thespecified threshold. For events of level equal or above thethreshold,
ThresholdFilterwill respond NEUTRAL whenits
decide() method is invoked. However, events witha level below
the threshold will be denied. Here is a sampleconfiguration file.
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <!-- deny all events with a level below INFO, that is TRACE and DEBUG --> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <encoder> <pattern> %-4relative [%thread] %-5level %logger{30} - %msg%n </pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="CONSOLE" /> </root> </configuration>
通过使用上面的两个过滤器,我们很容易得到想要的结果配置:
<configuration scan="false" debug="false"> <property name="module_name" value="happy-crawler-1688" /> <timestamp key="time_pattern" datePattern="yyyy-MM-dd HH:mm:ss" /> <contextName>${module_name}-${time_pattern}</contextName> <appender name="DEBUG_FILE" class="ch.qos.logback.core.FileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>DENY</onMatch> <onMismatch>NEUTRAL</onMismatch> </filter> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>NEUTRAL</onMismatch> </filter> <file>logs/debug.log</file> <append>false</append> <encoder> <pattern>%d [%thread] [%-5level] %logger{0} - %msg%n</pattern> </encoder> </appender> <appender name="WARN_FILE" class="ch.qos.logback.core.FileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>WARN</level> </filter> <file>logs/warn.log</file> <append>false</append> <encoder> <pattern>%d [%thread] [%-5level] %file,%line - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="DEBUG_FILE" /> <appender-ref ref="WARN_FILE" /> </root> </configuration>
参考文章: http://logback.qos.ch/manual/filters.html
相关文章推荐
- ABP框架的基础配置及依赖注入讲解
- java大数据两集合的交集
- Android下新增api编译问题
- 汉诺塔问题--算法
- 在PPT中实现对象翻转
- ,谈谈final, finally, finalize的区别
- selectOnFocus
- 触发器
- 解决mysql 远程链接问题
- ubuntu下安装程序的三种方法
- 解决“启动程序失败,路径或者权限错误”的执行问题
- nginx配置详解之main模块
- EditText
- Maven项目中使用JUnit进行单元测试
- java之路 static
- mysql 超级管理员
- linux中fork()函数详解
- uiautomatorviewer介绍
- swift果冻效果
- Android IPC机制学习(Android艺术开发探索)