|
| 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 | + * <configuration> |
| 30 | + * <import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/> |
| 31 | + * <import class="ch.qos.logback.core.filter.EvaluatorFilter"/> |
| 32 | + * <import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/> |
| 33 | + * <import class="ch.qos.logback.core.ConsoleAppender"/> |
| 34 | + * |
| 35 | + * <appender name="CONSOLE" class="ConsoleAppender"> |
| 36 | + * <filter class="EvaluatorFilter"> |
| 37 | + * <evaluator class="ExceptionMatchEvaluator"> |
| 38 | + * <exceptionClass>java.lang.RuntimeException</exceptionClass> |
| 39 | + * </evaluator> |
| 40 | + * <OnMismatch>DENY</OnMismatch> |
| 41 | + * <OnMatch>NEUTRAL</OnMatch> |
| 42 | + * </filter> |
| 43 | + * |
| 44 | + * <encoder class="PatternLayoutEncoder"> |
| 45 | + * <pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n</pattern> |
| 46 | + * </encoder> |
| 47 | + * </appender> |
| 48 | + * |
| 49 | + * <root level="INFO"> |
| 50 | + * <appender-ref ref="CONSOLE"/> |
| 51 | + * </root> |
| 52 | + * </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 | +} |
0 commit comments