1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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 v1.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  public class ExceptionMatchEvaluator extends EventEvaluatorBase<ILoggingEvent> {
58  
59      String exceptionClass;
60      private boolean start = false;
61  
62      public void start() {
63          if (exceptionClass == null) {
64              addError("The exceptionClass must be set");
65              return;
66          }
67          start = true;
68      }
69  
70      public void stop() {
71          start = false;
72      }
73  
74      public boolean isStarted() {
75          return start;
76      }
77  
78      @Override
79      public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
80  
81          IThrowableProxy throwableProxy = event.getThrowableProxy();
82          if (throwableProxy == null) {
83              return false;
84          }
85          return throwableProxy.getClassName().equalsIgnoreCase(exceptionClass);
86      }
87  
88      public String getExceptionClass() {
89          return exceptionClass;
90      }
91  
92      public void setExceptionClass(String exceptionClass) {
93          this.exceptionClass = exceptionClass;
94      }
95  
96  }