您的位置:首页 > 其它

logback 实现debug 日志和warn(error)日志分开打印

2016-06-15 17:42 435 查看
最近使用SLF4J 顺便迷上了logback, 想实现以前log4j那种 debug 和 warn(error) 日志分开打印到不同文件的方式,配置了半天没有成功,最后查找官方文档发现Filter可以用来实现我们想要的功能。

1 日志打印流程

 


2 我们这里主要使用在Appender格式化message string 之前的filter来实现想要的功能。

Regular logback-classic filters extend the
Filter
abstract class which essentially consists of a single
decide()
method taking an
ILoggingEvent
instance 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
FilterReply
enumeration
values, i.e. one of
DENY
,
NEUTRAL
or
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
Appender
instances. 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

LevelFilter
filters 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

The
ThresholdFilter
filters events below thespecified threshold. For events of level equal or above thethreshold,
ThresholdFilter
will 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: