1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.boolex;
16  
17  import ch.qos.logback.classic.spi.ILoggingEvent;
18  import ch.qos.logback.classic.spi.IThrowableProxy;
19  import ch.qos.logback.core.boolex.EvaluationException;
20  import ch.qos.logback.core.boolex.EventEvaluatorBase;
21  
22  /**
23   * A simple {@link ch.qos.logback.core.boolex.EventEvaluator} that checks whether the
24   * logging event being evaluated has a throwable of the same class as specified by the
25   * {@link #exceptionClass} parameter.
26   *
27   * <p>Here is a </p>
28   * <pre>
29   *  &lt;configuration>
30   *     &lt;import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
31   *     &lt;import class="ch.qos.logback.core.filter.EvaluatorFilter"/>
32   *     &lt;import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/>
33   *     &lt;import class="ch.qos.logback.core.ConsoleAppender"/>
34   *
35   *     &lt;appender name="CONSOLE" class="ConsoleAppender">
36   *         &lt;filter class="EvaluatorFilter">
37   *             &lt;evaluator class="ExceptionMatchEvaluator">
38   *                 &lt;exceptionClass>java.lang.RuntimeException&lt;/exceptionClass>
39   *             &lt;/evaluator>
40   *             &lt;OnMismatch>DENY&lt;/OnMismatch>
41   *             &lt;OnMatch>NEUTRAL&lt;/OnMatch>
42   *         &lt;/filter>
43   *
44   *         &lt;encoder class="PatternLayoutEncoder">
45   *             &lt;pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n&lt;/pattern>
46   *         &lt;/encoder>
47   *     &lt;/appender>
48   *
49   *     &lt;root level="INFO">
50   *         &lt;appender-ref ref="CONSOLE"/>
51   *     &lt;/root>
52   * &lt;/configuration>
53   *
54   *
55   * </pre>
56   *
57   * @since 1.5.15
58   */
59  public class ExceptionMatchEvaluator extends EventEvaluatorBase<ILoggingEvent> {
60  
61      String exceptionClass;
62      private boolean start = false;
63  
64      public void start() {
65          if (exceptionClass == null) {
66              addError("The exceptionClass must be set");
67              return;
68          }
69          start = true;
70      }
71  
72      public void stop() {
73          start = false;
74      }
75  
76      public boolean isStarted() {
77          return start;
78      }
79  
80      @Override
81      public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
82  
83          IThrowableProxy throwableProxy = event.getThrowableProxy();
84          if (throwableProxy == null) {
85              return false;
86          }
87          return throwableProxy.getClassName().equalsIgnoreCase(exceptionClass);
88      }
89  
90      public String getExceptionClass() {
91          return exceptionClass;
92      }
93  
94      public void setExceptionClass(String exceptionClass) {
95          this.exceptionClass = exceptionClass;
96      }
97  
98  }