2011-05-26 580 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来处理表达式解析。