Skip to content

Commit cb60369

Browse files
committed
addded StubEventEvaluator as default class for evaluator element so as to direct users to the JaninoEventEvaluator migration tool
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 1da2f17 commit cb60369

File tree

6 files changed

+222
-11
lines changed

6 files changed

+222
-11
lines changed

logback-classic-blackbox/src/test/blackboxInput/joran/callerData.xml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22
<!DOCTYPE configuration>
33

44
<configuration>
5+
<import class="ch.qos.logback.classic.blackbox.evaluator.MatchHelloEvaluator"/>
6+
<import class="ch.qos.logback.core.testUtil.StringListAppender"/>
57

6-
<evaluator name="helloEval">
7-
<Expression>m.matches(message)</Expression>
8-
<matcher>
9-
<name>m</name>
10-
<regex>^hello.*</regex>
11-
<CaseSensitive>false</CaseSensitive>
12-
</matcher>
8+
<evaluator name="helloEval" class="MatchHelloEvaluator">
9+
<checkForInclusion>hello</checkForInclusion>
1310
</evaluator>
1411

15-
<appender name="STR_LIST"
16-
class="ch.qos.logback.core.testUtil.StringListAppender">
12+
<appender name="STR_LIST" class="StringListAppender">
1713
<layout>
1814
<Pattern>%caller{4, helloEval}%d %level - %m%n</Pattern>
1915
</layout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2025, 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.blackbox.evaluator;
16+
17+
import ch.qos.logback.classic.spi.ILoggingEvent;
18+
import ch.qos.logback.core.boolex.EvaluationException;
19+
import ch.qos.logback.core.boolex.EventEvaluatorBase;
20+
21+
public class MatchHelloEvaluator extends EventEvaluatorBase<ILoggingEvent> {
22+
23+
String checkForInclusion;
24+
25+
public void start() {
26+
if (checkForInclusion != null) {
27+
start();
28+
}
29+
}
30+
31+
public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
32+
if (!isStarted()) {
33+
return false;
34+
}
35+
36+
String message = event.getMessage();
37+
boolean result = message.contains(checkForInclusion);
38+
return result;
39+
}
40+
41+
public String getCheckForInclusion() {
42+
return checkForInclusion;
43+
}
44+
45+
public void setCheckForInclusion(String checkForInclusion) {
46+
this.checkForInclusion = checkForInclusion;
47+
}
48+
49+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2025, 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.core.boolex.EvaluationException;
19+
import ch.qos.logback.core.boolex.EventEvaluatorBase;
20+
import ch.qos.logback.core.boolex.Matcher;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
public class StubEventEvaluator extends EventEvaluatorBase<ILoggingEvent> {
26+
27+
static public final String MSG_0 = "This class is a stub for JaninoEventEvaluator which was removed in logback version 1.5.13";
28+
static public final String MSG_1 = "You can migrate existing configurations to Java-only equivalents with the \"Janino Expression migrator\" tool at:";
29+
static public final String MSG_2 ="https://logback.qos.ch/translator/services/janinoExpressionMigrator.html";
30+
31+
protected List<Matcher> matcherList = new ArrayList<>();
32+
String expression;
33+
34+
@Override
35+
public void start() {
36+
stop();
37+
addWarn(MSG_0);
38+
addWarn(MSG_1);
39+
addWarn(MSG_2);
40+
}
41+
42+
@Override
43+
public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
44+
return false;
45+
}
46+
47+
public String getExpression() {
48+
return expression;
49+
}
50+
51+
public void setExpression(String expression) {
52+
this.expression = expression;
53+
}
54+
55+
public void addMatcher(Matcher matcher) {
56+
matcherList.add(matcher);
57+
}
58+
59+
}

logback-classic/src/main/java/ch/qos/logback/classic/model/processor/LogbackClassicDefaultNestedComponentRules.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818

1919
import ch.qos.logback.classic.PatternLayout;
20+
import ch.qos.logback.classic.boolex.StubEventEvaluator;
2021
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
2122
import ch.qos.logback.core.AppenderBase;
2223
import ch.qos.logback.core.UnsynchronizedAppenderBase;
@@ -48,6 +49,7 @@ static public void addDefaultNestedComponentRegistryRules(DefaultNestedComponent
4849

4950
registry.add(AppenderBase.class, "encoder", PatternLayoutEncoder.class);
5051
registry.add(UnsynchronizedAppenderBase.class, "encoder", PatternLayoutEncoder.class);
52+
registry.add(EvaluatorFilter.class, "evaluator", StubEventEvaluator.class);
5153

5254
SSLNestedComponentRegistryRules.addDefaultNestedComponentRegistryRules(registry);
5355
}
@@ -63,8 +65,7 @@ public static List<ParentTag_Tag_Class_Tuple> createTuplesList() {
6365
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "keyStore", KeyStoreFactoryBean.class.getName()));
6466
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "trustStore", KeyManagerFactoryFactoryBean.class.getName()));
6567
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "keyManagerFactory", SSLParametersConfiguration.class.getName()));
66-
tupleList
67-
.add(new ParentTag_Tag_Class_Tuple("ssl", "trustManagerFactory", TrustManagerFactoryFactoryBean.class.getName()));
68+
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "trustManagerFactory", TrustManagerFactoryFactoryBean.class.getName()));
6869
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "secureRandom", SecureRandomFactoryBean.class.getName()));
6970
return tupleList;
7071

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE configuration>
3+
4+
<configuration>
5+
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
6+
<import class="ch.qos.logback.core.filter.EvaluatorFilter"/>
7+
<import class="ch.qos.logback.core.read.ListAppender"/>
8+
9+
<appender name="LIST" class="ListAppender">
10+
<filter class="EvaluatorFilter">
11+
<evaluator>
12+
<expression>message.contains("billing")</expression>
13+
</evaluator>
14+
<onMismatch>NEUTRAL</onMismatch>
15+
<onMatch>DENY</onMatch>
16+
</filter>
17+
<encoder class="PatternLayoutEncoder">
18+
<pattern>%-4relative [%thread] %-5level %logger - %msg%n</pattern>
19+
</encoder>
20+
</appender>
21+
22+
<root level="DEBUG">
23+
<appender-ref ref="LIST"/>
24+
</root>
25+
</configuration>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2025, 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.joran.sanity;
16+
17+
import ch.qos.logback.classic.ClassicTestConstants;
18+
import ch.qos.logback.classic.Logger;
19+
import ch.qos.logback.classic.LoggerContext;
20+
import ch.qos.logback.classic.boolex.StubEventEvaluator;
21+
import ch.qos.logback.classic.joran.JoranConfigurator;
22+
import ch.qos.logback.classic.spi.ILoggingEvent;
23+
import ch.qos.logback.classic.util.LogbackMDCAdapter;
24+
import ch.qos.logback.core.joran.spi.JoranException;
25+
import ch.qos.logback.core.read.ListAppender;
26+
import ch.qos.logback.core.status.testUtil.StatusChecker;
27+
import ch.qos.logback.core.util.StatusPrinter2;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
31+
import org.slf4j.ILoggerFactory;
32+
import org.slf4j.spi.MDCAdapter;
33+
34+
import java.util.List;
35+
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
38+
public class EvaluatorStubTest {
39+
LoggerContext loggerContext = new LoggerContext();
40+
JoranConfigurator jc = new JoranConfigurator();
41+
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
42+
StatusChecker statusChecker = new StatusChecker(loggerContext);
43+
MDCAdapter mdcAdapter = new LogbackMDCAdapter();
44+
45+
@BeforeEach
46+
void setUp() {
47+
loggerContext.setMDCAdapter(mdcAdapter);
48+
}
49+
50+
@Test
51+
public void standaloneEventEvaluatorTest() throws JoranException {
52+
jc.setContext(loggerContext);
53+
jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "simpleEvaluator.xml");
54+
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_0);
55+
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_1);
56+
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_2);
57+
//statusPrinter2.print(loggerContext);
58+
}
59+
60+
@Test
61+
public void eventEvaluatorEmbeddedInFilterTest() throws JoranException {
62+
jc.setContext(loggerContext);
63+
jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "basicEventEvaluator.xml");
64+
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_0);
65+
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_1);
66+
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_2);
67+
68+
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
69+
70+
ListAppender listAppender = (ListAppender) root.getAppender("LIST");
71+
List<ILoggingEvent> eventList = listAppender.list;
72+
73+
String message = "hello";
74+
Logger logger = loggerContext.getLogger(this.getClass());
75+
logger.warn(message);
76+
assertEquals(1, eventList.size());
77+
assertEquals(message, eventList.get(0).getMessage());
78+
statusPrinter2.print(loggerContext);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)