Skip to content

Commit 020610a

Browse files
committed
name is optional in EventEvaluator, add ExceptionMatchEvaluator with a test
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 11a3951 commit 020610a

File tree

4 files changed

+158
-2
lines changed

4 files changed

+158
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration>
3+
4+
<!--
5+
~ Logback: the reliable, generic, fast and flexible logging framework.
6+
~ Copyright (C) 1999-2024, QOS.ch. All rights reserved.
7+
~
8+
~ This program and the accompanying materials are dual-licensed under
9+
~ either the terms of the Eclipse Public License v1.0 as published by
10+
~ the Eclipse Foundation
11+
~
12+
~ or (per the licensee's choosing)
13+
~
14+
~ under the terms of the GNU Lesser General Public License version 2.1
15+
~ as published by the Free Software Foundation.
16+
-->
17+
18+
<configuration>
19+
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
20+
<import class="ch.qos.logback.core.filter.EvaluatorFilter"/>
21+
<import class="ch.qos.logback.classic.boolex.ExceptionMatchEvaluator"/>
22+
<import class="ch.qos.logback.core.read.ListAppender"/>
23+
24+
<appender name="LIST" class="ListAppender">
25+
<filter class="EvaluatorFilter">
26+
<evaluator class="ExceptionMatchEvaluator">
27+
<exceptionClass>java.lang.RuntimeException</exceptionClass>
28+
</evaluator>
29+
<OnMismatch>DENY</OnMismatch>
30+
<OnMatch>NEUTRAL</OnMatch>
31+
</filter>
32+
33+
<encoder class="PatternLayoutEncoder">
34+
<pattern>%-4relative [%thread] %-5level %logger -%kvp -%msg%n</pattern>
35+
</encoder>
36+
</appender>
37+
38+
<root level="INFO">
39+
<appender-ref ref="LIST"/>
40+
</root>
41+
</configuration>

logback-classic/src/test/java/ch/qos/logback/classic/joran/JoranConfiguratorTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ch.qos.logback.classic.model.ConfigurationModel;
1919
import ch.qos.logback.classic.model.LoggerModel;
2020
import ch.qos.logback.classic.spi.ILoggingEvent;
21+
import ch.qos.logback.classic.spi.LoggingEvent;
2122
import ch.qos.logback.classic.turbo.DebugUsersTurboFilter;
2223
import ch.qos.logback.classic.turbo.NOPTurboFilter;
2324
import ch.qos.logback.classic.turbo.TurboFilter;
@@ -757,6 +758,24 @@ public void levelFromAPropertyTest() throws JoranException {
757758

758759
}
759760

761+
@Test
762+
public void exceptionEventFilter() throws JoranException {
763+
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "boolex/exceptionEvaluator.xml");
764+
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
765+
root.info("deny");
766+
root.info("allow", new RuntimeException("test"));
767+
768+
ListAppender<ILoggingEvent> listAppender = (ListAppender<ILoggingEvent>) root.getAppender("LIST");
769+
assertNotNull(listAppender);
770+
assertEquals(1, listAppender.list.size());
771+
772+
ILoggingEvent le = listAppender.list.get(0);
773+
774+
assertNotNull(le.getThrowableProxy());
775+
assertEquals(RuntimeException.class.getName(), le.getThrowableProxy().getClassName());
776+
777+
}
778+
760779
@Test
761780
public void modelSerialization() throws JoranException, IOException, ClassNotFoundException {
762781
String outputPath = OUTPUT_DIR_PREFIX + "minimal_" + diff + MODEL_CONFIG_FILE_EXTENSION;

logback-core/src/main/java/ch/qos/logback/core/boolex/EventEvaluator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public interface EventEvaluator<E> extends ContextAware, LifeCycle {
4444
boolean evaluate(E event) throws NullPointerException, EvaluationException;
4545

4646
/**
47-
* Evaluators are named entities.
47+
* An evaluator may optionally have a name.
4848
*
4949
* @return The name of this evaluator.
5050
*/
5151
String getName();
5252

5353
/**
54-
* Evaluators are named entities.
54+
* An evaluator may optionally have a name.
5555
*/
5656
void setName(String name);
5757
}

0 commit comments

Comments
 (0)