2011-05-26 576 views

回答

15

你可以用一個簡單的EvaluatorFilter做到這一點:

<filter class="ch.qos.logback.core.filter.EvaluatorFilter"> 
    <evaluator> 
     <expression>java.lang.RuntimeException.class.isInstance(throwable)</expression> 
    </evaluator> 
    <onMatch>DENY</onMatch> 
</filter> 

請注意,你需要在你pom.xml以下依賴,以及:

<dependency> 
    <groupId>org.codehaus.janino</groupId> 
    <artifactId>janino</artifactId> 
    <version>2.5.16</version> 
</dependency> 

另一種可能的解決方案是一個自定義Filter實現:

import ch.qos.logback.classic.spi.ILoggingEvent; 
import ch.qos.logback.classic.spi.IThrowableProxy; 
import ch.qos.logback.classic.spi.ThrowableProxy; 
import ch.qos.logback.core.filter.Filter; 
import ch.qos.logback.core.spi.FilterReply; 

public class SampleFilter extends Filter<ILoggingEvent> { 

    private Class<?> exceptionClass; 

    public SampleFilter() { 
    } 

    @Override 
    public FilterReply decide(final ILoggingEvent event) { 
     final IThrowableProxy throwableProxy = event.getThrowableProxy(); 
     if (throwableProxy == null) { 
      return FilterReply.NEUTRAL; 
     } 

     if (!(throwableProxy instanceof ThrowableProxy)) { 
      return FilterReply.NEUTRAL; 
     } 

     final ThrowableProxy throwableProxyImpl = 
      (ThrowableProxy) throwableProxy; 
     final Throwable throwable = throwableProxyImpl.getThrowable(); 
     if (exceptionClass.isInstance(throwable)) { 
      return FilterReply.DENY; 
     } 

     return FilterReply.NEUTRAL; 
    } 

    public void setExceptionClassName(final String exceptionClassName) { 
     try { 
      exceptionClass = Class.forName(exceptionClassName); 
     } catch (final ClassNotFoundException e) { 
      throw new IllegalArgumentException("Class is unavailable: " 
        + exceptionClassName, e); 
     } 
    } 
} 

Wi TH適當配置:

<filter class="hu.palacsint.logbacktest.SampleFilter"> 
    <exceptionClassName>java.lang.Exception</exceptionClassName> 
</filter> 

TurboFilteryou get the thrown Throwable instance directly,這樣你就可以調用isInstance方法,無需手工鑄造IThrowableProxyThrowableProxy

Further documentation

+0

謝謝,我自那時起就圍繞這個特定問題開展工作,但這對未來來說是很好的信息。 – 2011-09-12 15:41:28

1

這個問題是5歲,但我提供我發現只是爲了保持其最新的解決方案。

我發現在官方文檔的解決方案: http://logback.qos.ch/manual/layouts.html

對於我的特定情況下,它是運行普通的舊的Servlet一個17歲的網站(啊,天),該servlet現在拋出異常。如果用戶沒有登錄,因此,這裏是我的代碼片段:

的Servlet

try { 
    InvalidLoginException.throwIfBadLogin(webUser); 

    // main logic here 

    } catch (InvalidLoginException e) { 
    throw e; 
    } catch (Throwable t) { 
    log.error(t); 
    throw new UnhandledException(t); 
    } 

的web.xml

<error-page> 
    <exception-type>com.mycompany.servlet.exception.InvalidLoginException</exception-type> 
    <location>/servlet/Login</location> 
</error-page> 

從上面設置的,我不想記錄此例外,因爲它不是真正一個例外,而是在邏輯中斷重定向到登錄。

所以,我logback.xml文件的開頭是這樣的:

<configuration packagingData="true" scan="true" debug="true" scanPeriod="30 seconds"> 
    <evaluator name="InvalidLoginExceptionSuppressor"> 
     <expression>throwable != null &amp;&amp; throwable instanceof com.mycompany.servlet.exception.InvalidLoginException</expression> 
    </evaluator> 

,並在logback.xml文件進一步下跌,我的appender:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder> 
     <pattern>%d{MM-dd HH:mm:ss.SSS} %-5level %logger{36} - %msg %ex{full,InvalidLoginExceptionSuppressor}%n</pattern> 
     <immediateFlush>false</immediateFlush> 
    </encoder> 

另外請注意,爲了這項工作,我必須包含janino來處理表達式解析。