-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
name is optional in EventEvaluator, add ExceptionMatchEvaluator with …
…a test Signed-off-by: Ceki Gulcu <[email protected]>
- Loading branch information
Showing
4 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
logback-classic/src/main/java/ch/qos/logback/classic/boolex/ExceptionMatchEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Logback: the reliable, generic, fast and flexible logging framework. | ||
* Copyright (C) 1999-2024, QOS.ch. All rights reserved. | ||
* | ||
* This program and the accompanying materials are dual-licensed under | ||
* either the terms of the Eclipse Public License v1.0 as published by | ||
* the Eclipse Foundation | ||
* | ||
* or (per the licensee's choosing) | ||
* | ||
* under the terms of the GNU Lesser General Public License version 2.1 | ||
* as published by the Free Software Foundation. | ||
*/ | ||
|
||
package ch.qos.logback.classic.boolex; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
import ch.qos.logback.core.boolex.EvaluationException; | ||
import ch.qos.logback.core.boolex.EventEvaluatorBase; | ||
|
||
/** | ||
* A simple {@link ch.qos.logback.core.boolex.EventEvaluator} that checks whether the | ||
* logging event being evaluated has a throwable of the same class as specified by the | ||
* {@link #exceptionClass} parameter. | ||
* | ||
* <p>Here is a </p> | ||
* <pre> | ||
* <configuration> | ||
* <import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/> | ||
* <import class="ch.qos.logback.core.filter.EvaluatorFilter"/> | ||
* <import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/> | ||
* <import class="ch.qos.logback.core.ConsoleAppender"/> | ||
* | ||
* <appender name="CONSOLE" class="ConsoleAppender"> | ||
* <filter class="EvaluatorFilter"> | ||
* <evaluator class="ExceptionMatchEvaluator"> | ||
* <exceptionClass>java.lang.RuntimeException</exceptionClass> | ||
* </evaluator> | ||
* <OnMismatch>DENY</OnMismatch> | ||
* <OnMatch>NEUTRAL</OnMatch> | ||
* </filter> | ||
* | ||
* <encoder class="PatternLayoutEncoder"> | ||
* <pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n</pattern> | ||
* </encoder> | ||
* </appender> | ||
* | ||
* <root level="INFO"> | ||
* <appender-ref ref="CONSOLE"/> | ||
* </root> | ||
* </configuration> | ||
* | ||
* | ||
* </pre> | ||
*/ | ||
public class ExceptionMatchEvaluator extends EventEvaluatorBase<ILoggingEvent> { | ||
|
||
String exceptionClass; | ||
private boolean start = false; | ||
|
||
public void start() { | ||
if (exceptionClass == null) { | ||
addError("The exceptionClass must be set"); | ||
return; | ||
} | ||
start = true; | ||
} | ||
|
||
public void stop() { | ||
start = false; | ||
} | ||
|
||
public boolean isStarted() { | ||
return start; | ||
} | ||
|
||
@Override | ||
public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException { | ||
|
||
IThrowableProxy throwableProxy = event.getThrowableProxy(); | ||
if (throwableProxy == null) { | ||
return false; | ||
} | ||
return throwableProxy.getClassName().equalsIgnoreCase(exceptionClass); | ||
} | ||
|
||
public String getExceptionClass() { | ||
return exceptionClass; | ||
} | ||
|
||
public void setExceptionClass(String exceptionClass) { | ||
this.exceptionClass = exceptionClass; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
logback-classic/src/test/input/joran/boolex/exceptionEvaluator.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE configuration> | ||
|
||
<!-- | ||
~ Logback: the reliable, generic, fast and flexible logging framework. | ||
~ Copyright (C) 1999-2024, QOS.ch. All rights reserved. | ||
~ | ||
~ This program and the accompanying materials are dual-licensed under | ||
~ either the terms of the Eclipse Public License v1.0 as published by | ||
~ the Eclipse Foundation | ||
~ | ||
~ or (per the licensee's choosing) | ||
~ | ||
~ under the terms of the GNU Lesser General Public License version 2.1 | ||
~ as published by the Free Software Foundation. | ||
--> | ||
|
||
<configuration> | ||
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/> | ||
<import class="ch.qos.logback.core.filter.EvaluatorFilter"/> | ||
<import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/> | ||
<import class="ch.qos.logback.core.read.ListAppender"/> | ||
|
||
<appender name="LIST" class="ListAppender"> | ||
<filter class="EvaluatorFilter"> | ||
<evaluator class="ExceptionMatchEvaluator"> | ||
<exceptionClass>java.lang.RuntimeException</exceptionClass> | ||
</evaluator> | ||
<OnMismatch>DENY</OnMismatch> | ||
<OnMatch>NEUTRAL</OnMatch> | ||
</filter> | ||
|
||
<encoder class="PatternLayoutEncoder"> | ||
<pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="LIST"/> | ||
</root> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
020610a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
ExceptionMatchEvaluator
be documented as@since 1.5.15
?020610a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ppkarwasz Yes, it should. Thank you for the heads up.