电竞比分网-中国电竞赛事及体育赛事平台

分享

logback-Filter機(jī)制

 CevenCheng 2010-09-19

logback-Filter機(jī)制

關(guān)鍵字: logback

http://logback./manual/filters.html 譯自官方文檔。其實(shí)看看也不是很難懂,就是看過(guò)后動(dòng)手寫(xiě)一次,可能會(huì)加強(qiáng)印象吧。最近被log4j整郁悶了, 針對(duì)多樣化的分類輸出無(wú)所適從,只能開(kāi)多個(gè)寫(xiě)死的appender來(lái)湊合。

 

 

As we have seen, logback has several built-in ways for filtering log requests, including the context-wide filter, logger-level selection rule and appender filters. These provide high performance filtering for the most commonly encountered cases. These filters are largely inspired from Linux ipchains or iptables as they are called in more recent Linux kernels. Logback filters are based on ternary logic allowing them to be assembled or chained together to compose an arbitrarily complex filtering policy.

 

logback提供了一些自帶的過(guò)濾機(jī)制,包含上下文過(guò)濾,日志級(jí)別過(guò)濾,Appender輸出過(guò)濾等。他們提供了高性能的、滿足最常用場(chǎng)景的過(guò)濾實(shí)現(xiàn)。 這些過(guò)濾機(jī)制很大程度上來(lái)源自linux中ipchains/iptables的靈感。Logback提供的Filter可以通過(guò)三元邏輯運(yùn)算來(lái)組合過(guò)濾鏈來(lái)實(shí)現(xiàn)復(fù)雜的過(guò)濾需求。

 

There are two main types of filters, namely Filter and TurboFilter .

重點(diǎn)是兩種抽象的Filter類型,filters,TurboFilter.

 

Logback Classic

 

Filter-chain

Filters are based on ternary logic. The decide(Object event) method of each filter is called in sequence. This method returns one of the FilterReply enumeration values, i.e. one of FilterReply.DENY , FilterReply.NEUTRAL or FilterReply.ACCEPT . If the returned value is FilterReply.DENY , then the log event is dropped immediately without consulting the remaining filters. If the value returned is FilterReply.NEUTRAL , then the next filter in the chain is consulted. If there are no further filters to consult, then the logging event is processed normally. If the returned value is FilterReply.ACCEPT , then the logging event is processed immediately skipping the remaining filters.

 

過(guò)濾器是串行工作的,有3種FilterReply結(jié)果,DENY,NEUTRAL,ACCEPT.他們之間通過(guò)邏輯運(yùn)算來(lái)得到最終處理過(guò)程。

 

Implementing your own Filter

Creating your own filter is not difficult. All you have to do is extend the Filter abstract class. The only method that you will have to implement is the decide() method, allowing you to contentrate only on the behaviour of your filter.

The next class is all it takes to implement one's own filter. All it does is accept logging events who's message contains the Stringsample . The filter will give a neutral response to any logging event who's message does not contain this String.

Example 6.1: Basic custom filter (logback-examples/src/main/java/chapter6/SampleFilter.java )

我們簡(jiǎn)單的實(shí)現(xiàn)過(guò)濾類,

Java代碼 
  1. package chapter6;  
  2.   
  3. import ch.qos.logback.classic.spi.LoggingEvent;  
  4. import ch.qos.logback.core.filter.Filter;  
  5. import ch.qos.logback.core.spi.FilterReply;  
  6.   
  7. public class SampleFilter extends Filter {  
  8.   
  9.   @Override  
  10.   public FilterReply decide(Object eventObject) {  
  11.     LoggingEvent event = (LoggingEvent)eventObject;  
  12.       
  13.     if (event.getMessage().contains("sample")) {  
  14.       return FilterReply.ACCEPT;  
  15.     } else {  
  16.       return FilterReply.NEUTRAL;  
  17.     }  
  18.   }  
  19. }  

 然后配置如下:

Xml代碼 
  1. <configuration>  
  2.   <appender name="STDOUT"  
  3.     class="ch.qos.logback.core.ConsoleAppender">  
  4.     <Filter class="chapter6.SampleFilter" />  
  5.   
  6.     <layout class="ch.qos.logback.classic.PatternLayout">  
  7.       <pattern>  
  8.         %-4relative [%thread] %-5level %logger - %msg%n  
  9.       </pattern>  
  10.     </layout>  
  11.   </appender>  
  12.           
  13.   <root>  
  14.     <appender-ref ref="STDOUT" />  
  15.   </root>  
  16. </configuration>  

 

Logback Filters

1.At the moment, there are two filters that ship with logback. LevelFilter provides event filtering based on a Level value. If the event's level is equal to the configured level, the filter accepts or denies the event, depending on its configuration. It allows you to choose the behaviour of logback for a precise given level.  級(jí)別過(guò)濾Filter。相等判斷==

Xml代碼 
  1. <filter class="ch.qos.logback.classic.filter.LevelFilter">  
  2.   <level>INFO</level>  
  3.   <onMatch>ACCEPT</onMatch>  
  4.   <onMismatch>DENY</onMismatch>  
  5. </filter>  
 

2.The second filter that ships with logback is ThresholdFilter . It is also based on level value, but acts as a threshold to deny any request whose level is not equal or greater to the configured level. A sample use of the ThresholdFilter is shown below. 級(jí)別過(guò)濾,臨界點(diǎn)判斷,>=

Xml代碼 
  1. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
  2.   <level>INFO</level>  
  3. </filter>  
 

Evaluator Filters taking Java Expressions 正則式。。標(biāo)配

文檔用圖標(biāo)列出了可以匹配的內(nèi)容,請(qǐng)查閱源文檔頁(yè)面

Java代碼 
  1. <configuration>  
  2.   
  3.   <appender name="STDOUT"  
  4.     class="ch.qos.logback.core.ConsoleAppender">  
  5.     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  6.       <evaluator name="myEval">  
  7.         <expression>message.contains("billing")</expression>  
  8.       </evaluator>  
  9.       <OnMismatch>NEUTRAL</OnMismatch>  
  10.       <OnMatch>DENY</OnMatch>  
  11.     </filter>  
  12.     <layout>  
  13.       <pattern>  
  14.         %-4relative [%thread] %-5level %logger - %msg%n  
  15.       </pattern>  
  16.     </layout>  
  17.   </appender>  
  18.   
  19.   <root level="INFO">  
  20.     <appender-ref ref="STDOUT" />  
  21.   </root>  
  22. </configuration>  
 

TurboFilters

TurboFilter objects all extend the TurboFilter abstract class. Like the regular filters, they use ternary logic to return their evaluation of the logging event.

Overall, they work much like the previously mentionned filters. However, there are two main differences between Filter andTurboFilter objects.

TuboFilter跟Filter有兩個(gè)主要區(qū)別:

 

TurboFilter objects are tied to the logging context. Hence, they are called not only when a given appender is used, but each and every time a logging request is issued. Their scope is wider than appender-attached filters.

1.TurboFilter會(huì)試圖記錄上下文環(huán)境。因此他們會(huì)在每次logging請(qǐng)求產(chǎn)生的時(shí)候調(diào)用,而不是一個(gè)指定的appender使用時(shí)才出現(xiàn)。

 

More importantly, they are called before the LoggingEvent object creation. TurboFilter objects do not require the instantiation of a logging event to filter a logging request. As such, turbo filters are intended for high performance filtering of logging event, even before they are created

2.更重要的是,TurboFilter會(huì)在日志事件對(duì)象創(chuàng)建前調(diào)用。因此它具有更高性能的過(guò)濾日志事件,即使在事件被創(chuàng)建之前。

Java代碼 
  1. package chapter6;  
  2.   
  3. import org.slf4j.Marker;  
  4. import org.slf4j.MarkerFactory;  
  5.   
  6. import ch.qos.logback.classic.Level;  
  7. import ch.qos.logback.classic.Logger;  
  8. import ch.qos.logback.classic.turbo.TurboFilter;  
  9. import ch.qos.logback.core.spi.FilterReply;  
  10.   
  11. public class SampleTurboFilter extends TurboFilter {  
  12.   
  13.   String marker;  
  14.   Marker markerToAccept;  
  15.   
  16.   @Override  
  17.   public FilterReply decide(Marker marker, Logger logger, Level level,  
  18.       String format, Object[] params, Throwable t) {  
  19.   
  20.     if (!isStarted()) {  
  21.       return FilterReply.NEUTRAL;  
  22.     }  
  23.   
  24.     if ((markerToAccept.equals(marker))) {  
  25.       return FilterReply.ACCEPT;  
  26.     } else {  
  27.       return FilterReply.NEUTRAL;  
  28.     }  
  29.   }  
  30.   
  31.   public String getMarker() {  
  32.     return marker;  
  33.   }  
  34.   
  35.   public void setMarker(String markerStr) {  
  36.     this.marker = markerStr;  
  37.   }  
  38.   
  39.   @Override  
  40.   public void start() {  
  41.     if (marker != null && marker.trim().length() > 0) {  
  42.       markerToAccept = MarkerFactory.getMarker(marker);  
  43.       super.start();   
  44.     }  
  45.   }  
  46. }  

 上例對(duì)具體的maker做了過(guò)濾,配置當(dāng)然簡(jiǎn)單

Xml代碼 
  1. <configuration>  
  2.   <turboFilter class="chapter6.SampleTurboFilter">  
  3.     <Marker>sample</Marker>  
  4.   </turboFilter>  
  5.   
  6.   <appender name="STDOUT"  
  7.     class="ch.qos.logback.core.ConsoleAppender">  
  8.     <layout class="ch.qos.logback.classic.PatternLayout">  
  9.       <pattern>  
  10.         %-4relative [%thread] %-5level %logger - %msg%n  
  11.       </pattern>  
  12.     </layout>  
  13.   </appender>  
  14.   
  15.   <root>  
  16.     <appender-ref ref="STDOUT" />  
  17.   </root>  
  18. </configuration>  

 Logback classic ships with several TurboFilter classes ready for use. The MDCFilter check the presence of a given value in the MDC whereas DynamicThresholdFilter allows filtering based on MDC key/level thresold associations. On the other hand,MarkerFilter checks for the presence of a specific marker associated with the logging request.

 

Logback已經(jīng)實(shí)現(xiàn)了3個(gè)基本的TurboFilter,MDCFilter  DynamicThresholdFilter  MarkerFilter

Xml代碼 
  1. <turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">  
  2.   <MDCKey>username</MDCKey>  
  3.   <Value>sebastien</Value>  
  4.   <OnMatch>ACCEPT</OnMatch>  
  5. </turboFilter>  
  6.         
  7. <turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">  
  8.   <Marker>billing</Marker>  
  9.   <OnMatch>DENY</OnMatch>  
  10. </turboFilter>  
 

Logback-access 記錄操作

Logback-access offers most of the features available with logback-classic. Filter objects are available and work in the same way as their logback-classic counterparts. They handle access' implementation of logging events: AccessEvent . Thus, a customized filter for logback access follows strictly the same rules as those for logback-classic, except for the event type recieved as parameter. On the other hand, TurboFilter objects are supported by logback-access.

 

一個(gè)可以保證所有404錯(cuò)誤都會(huì)被記錄的例子?。。ㄟ@個(gè)很有效)

Xml代碼 
  1. <configuration>  
  2.   
  3.   <appender name="STDOUT"  
  4.     class="ch.qos.logback.core.ConsoleAppender">  
  5.     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  6.       <evaluator name="myEval">  
  7.         <expression>event.getStatusCode() == 404</expression>  
  8.       </evaluator>  
  9.       <OnMismatch>NEUTRAL</OnMismatch>  
  10.       <OnMatch>ACCEPT</OnMatch>  
  11.     </filter>  
  12.     <layout class="ch.qos.logback.access.PatternLayout">  
  13.       <pattern>  
  14.         %h %l %u %t %r %s %b  
  15.       </pattern>  
  16.     </layout>  
  17.   </appender>  
  18.   
  19.   <appender-ref ref="STDOUT" />  
  20. </configuration>  

 更高級(jí)的示例:記錄不返回css資源的所有404

Xml代碼 
  1. <configuration>  
  2.   
  3.   <appender name="STDOUT"  
  4.     class="ch.qos.logback.core.ConsoleAppender">  
  5.     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  6.       <evaluator name="Eval404">  
  7.         <expression>event.getStatusCode() == 404</expression>  
  8.       </evaluator>  
  9.       <OnMismatch>NEUTRAL</OnMismatch>  
  10.       <OnMatch>ACCEPT</OnMatch>  
  11.     </filter>  
  12.     <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
  13.       <evaluator name="EvalCSS">  
  14.         <expression>event.getRequestURI().contains("css")</expression>  
  15.       </evaluator>  
  16.       <OnMismatch>NEUTRAL</OnMismatch>  
  17.       <OnMatch>DENY</OnMatch>  
  18.     </filter>  
  19.     <layout class="ch.qos.logback.access.PatternLayout">  
  20.       <pattern>  
  21.         %h %l %u %t %r %s %b  
  22.       </pattern>  
  23.     </layout>  
  24.   </appender>  
  25.   
  26.   <appender-ref ref="STDOUT" />  
  27. </configuration>  
 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多