pal*_*int 22

你可以用一个简单的方法做到EvaluatorFilter:

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

请注意,您还需要以下依赖项pom.xml:

<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>2.5.16</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

另一种可能的解决方案是定制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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用适当的配置:

<filter class="hu.palacsint.logbacktest.SampleFilter">
    <exceptionClassName>java.lang.Exception</exceptionClassName>
</filter>
Run Code Online (Sandbox Code Playgroud)

在一个TurboFilter Throwable直接得到抛出的实例,所以你可以调用该isInstance方法而无需手动转换IThrowableProxyThrowableProxy.

进一步的文件