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 * <configuration> 030 * <import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/> 031 * <import class="ch.qos.logback.core.filter.EvaluatorFilter"/> 032 * <import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/> 033 * <import class="ch.qos.logback.core.ConsoleAppender"/> 034 * 035 * <appender name="CONSOLE" class="ConsoleAppender"> 036 * <filter class="EvaluatorFilter"> 037 * <evaluator class="ExceptionMatchEvaluator"> 038 * <exceptionClass>java.lang.RuntimeException</exceptionClass> 039 * </evaluator> 040 * <OnMismatch>DENY</OnMismatch> 041 * <OnMatch>NEUTRAL</OnMatch> 042 * </filter> 043 * 044 * <encoder class="PatternLayoutEncoder"> 045 * <pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n</pattern> 046 * </encoder> 047 * </appender> 048 * 049 * <root level="INFO"> 050 * <appender-ref ref="CONSOLE"/> 051 * </root> 052 * </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}