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 * <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 */ 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}