001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.classic.boolex;
016
017import ch.qos.logback.classic.spi.ILoggingEvent;
018import ch.qos.logback.classic.spi.IThrowableProxy;
019import ch.qos.logback.core.boolex.EvaluationException;
020import ch.qos.logback.core.boolex.EventEvaluatorBase;
021
022/**
023 * A simple {@link ch.qos.logback.core.boolex.EventEvaluator} that checks whether the
024 * logging event being evaluated has a throwable of the same class as specified by the
025 * {@link #exceptionClass} parameter.
026 *
027 * <p>Here is a </p>
028 * <pre>
029 *  &lt;configuration>
030 *     &lt;import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
031 *     &lt;import class="ch.qos.logback.core.filter.EvaluatorFilter"/>
032 *     &lt;import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/>
033 *     &lt;import class="ch.qos.logback.core.ConsoleAppender"/>
034 *
035 *     &lt;appender name="CONSOLE" class="ConsoleAppender">
036 *         &lt;filter class="EvaluatorFilter">
037 *             &lt;evaluator class="ExceptionMatchEvaluator">
038 *                 &lt;exceptionClass>java.lang.RuntimeException&lt;/exceptionClass>
039 *             &lt;/evaluator>
040 *             &lt;OnMismatch>DENY&lt;/OnMismatch>
041 *             &lt;OnMatch>NEUTRAL&lt;/OnMatch>
042 *         &lt;/filter>
043 *
044 *         &lt;encoder class="PatternLayoutEncoder">
045 *             &lt;pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n&lt;/pattern>
046 *         &lt;/encoder>
047 *     &lt;/appender>
048 *
049 *     &lt;root level="INFO">
050 *         &lt;appender-ref ref="CONSOLE"/>
051 *     &lt;/root>
052 * &lt;/configuration>
053 *
054 *
055 * </pre>
056 *
057 * @since 1.5.15
058 */
059public class ExceptionMatchEvaluator extends EventEvaluatorBase<ILoggingEvent> {
060
061    String exceptionClass;
062    private boolean start = false;
063
064    public void start() {
065        if (exceptionClass == null) {
066            addError("The exceptionClass must be set");
067            return;
068        }
069        start = true;
070    }
071
072    public void stop() {
073        start = false;
074    }
075
076    public boolean isStarted() {
077        return start;
078    }
079
080    @Override
081    public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
082
083        IThrowableProxy throwableProxy = event.getThrowableProxy();
084        if (throwableProxy == null) {
085            return false;
086        }
087        return throwableProxy.getClassName().equalsIgnoreCase(exceptionClass);
088    }
089
090    public String getExceptionClass() {
091        return exceptionClass;
092    }
093
094    public void setExceptionClass(String exceptionClass) {
095        this.exceptionClass = exceptionClass;
096    }
097
098}