001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2024, 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 v1.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 */
057public class ExceptionMatchEvaluator extends EventEvaluatorBase<ILoggingEvent> {
058
059    String exceptionClass;
060    private boolean start = false;
061
062    public void start() {
063        if (exceptionClass == null) {
064            addError("The exceptionClass must be set");
065            return;
066        }
067        start = true;
068    }
069
070    public void stop() {
071        start = false;
072    }
073
074    public boolean isStarted() {
075        return start;
076    }
077
078    @Override
079    public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
080
081        IThrowableProxy throwableProxy = event.getThrowableProxy();
082        if (throwableProxy == null) {
083            return false;
084        }
085        return throwableProxy.getClassName().equalsIgnoreCase(exceptionClass);
086    }
087
088    public String getExceptionClass() {
089        return exceptionClass;
090    }
091
092    public void setExceptionClass(String exceptionClass) {
093        this.exceptionClass = exceptionClass;
094    }
095
096}