Skip to content

Commit

Permalink
addded StubEventEvaluator as default class for evaluator element so a…
Browse files Browse the repository at this point in the history
…s to direct users to the JaninoEventEvaluator migration tool

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Jan 5, 2025
1 parent 1da2f17 commit cb60369
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
<!DOCTYPE configuration>

<configuration>
<import class="ch.qos.logback.classic.blackbox.evaluator.MatchHelloEvaluator"/>
<import class="ch.qos.logback.core.testUtil.StringListAppender"/>

<evaluator name="helloEval">
<Expression>m.matches(message)</Expression>
<matcher>
<name>m</name>
<regex>^hello.*</regex>
<CaseSensitive>false</CaseSensitive>
</matcher>
<evaluator name="helloEval" class="MatchHelloEvaluator">
<checkForInclusion>hello</checkForInclusion>
</evaluator>

<appender name="STR_LIST"
class="ch.qos.logback.core.testUtil.StringListAppender">
<appender name="STR_LIST" class="StringListAppender">
<layout>
<Pattern>%caller{4, helloEval}%d %level - %m%n</Pattern>
</layout>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2025, 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.blackbox.evaluator;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.boolex.EvaluationException;
import ch.qos.logback.core.boolex.EventEvaluatorBase;

public class MatchHelloEvaluator extends EventEvaluatorBase<ILoggingEvent> {

String checkForInclusion;

public void start() {
if (checkForInclusion != null) {
start();
}
}

public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
if (!isStarted()) {
return false;
}

String message = event.getMessage();
boolean result = message.contains(checkForInclusion);
return result;
}

public String getCheckForInclusion() {
return checkForInclusion;
}

public void setCheckForInclusion(String checkForInclusion) {
this.checkForInclusion = checkForInclusion;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2025, 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.core.boolex.EvaluationException;
import ch.qos.logback.core.boolex.EventEvaluatorBase;
import ch.qos.logback.core.boolex.Matcher;

import java.util.ArrayList;
import java.util.List;

public class StubEventEvaluator extends EventEvaluatorBase<ILoggingEvent> {

static public final String MSG_0 = "This class is a stub for JaninoEventEvaluator which was removed in logback version 1.5.13";
static public final String MSG_1 = "You can migrate existing configurations to Java-only equivalents with the \"Janino Expression migrator\" tool at:";
static public final String MSG_2 ="https://logback.qos.ch/translator/services/janinoExpressionMigrator.html";

protected List<Matcher> matcherList = new ArrayList<>();
String expression;

@Override
public void start() {
stop();
addWarn(MSG_0);
addWarn(MSG_1);
addWarn(MSG_2);
}

@Override
public boolean evaluate(ILoggingEvent event) throws NullPointerException, EvaluationException {
return false;
}

public String getExpression() {
return expression;
}

public void setExpression(String expression) {
this.expression = expression;
}

public void addMatcher(Matcher matcher) {
matcherList.add(matcher);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;

import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.boolex.StubEventEvaluator;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
Expand Down Expand Up @@ -48,6 +49,7 @@ static public void addDefaultNestedComponentRegistryRules(DefaultNestedComponent

registry.add(AppenderBase.class, "encoder", PatternLayoutEncoder.class);
registry.add(UnsynchronizedAppenderBase.class, "encoder", PatternLayoutEncoder.class);
registry.add(EvaluatorFilter.class, "evaluator", StubEventEvaluator.class);

SSLNestedComponentRegistryRules.addDefaultNestedComponentRegistryRules(registry);
}
Expand All @@ -63,8 +65,7 @@ public static List<ParentTag_Tag_Class_Tuple> createTuplesList() {
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "keyStore", KeyStoreFactoryBean.class.getName()));
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "trustStore", KeyManagerFactoryFactoryBean.class.getName()));
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "keyManagerFactory", SSLParametersConfiguration.class.getName()));
tupleList
.add(new ParentTag_Tag_Class_Tuple("ssl", "trustManagerFactory", TrustManagerFactoryFactoryBean.class.getName()));
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "trustManagerFactory", TrustManagerFactoryFactoryBean.class.getName()));
tupleList.add(new ParentTag_Tag_Class_Tuple("ssl", "secureRandom", SecureRandomFactoryBean.class.getName()));
return tupleList;

Expand Down
25 changes: 25 additions & 0 deletions logback-classic/src/test/input/joran/basicEventEvaluator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
<import class="ch.qos.logback.core.filter.EvaluatorFilter"/>
<import class="ch.qos.logback.core.read.ListAppender"/>

<appender name="LIST" class="ListAppender">
<filter class="EvaluatorFilter">
<evaluator>
<expression>message.contains("billing")</expression>
</evaluator>
<onMismatch>NEUTRAL</onMismatch>
<onMatch>DENY</onMatch>
</filter>
<encoder class="PatternLayoutEncoder">
<pattern>%-4relative [%thread] %-5level %logger - %msg%n</pattern>
</encoder>
</appender>

<root level="DEBUG">
<appender-ref ref="LIST"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2025, 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.joran.sanity;

import ch.qos.logback.classic.ClassicTestConstants;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.boolex.StubEventEvaluator;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.util.LogbackMDCAdapter;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.read.ListAppender;
import ch.qos.logback.core.status.testUtil.StatusChecker;
import ch.qos.logback.core.util.StatusPrinter2;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.slf4j.ILoggerFactory;
import org.slf4j.spi.MDCAdapter;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class EvaluatorStubTest {
LoggerContext loggerContext = new LoggerContext();
JoranConfigurator jc = new JoranConfigurator();
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
StatusChecker statusChecker = new StatusChecker(loggerContext);
MDCAdapter mdcAdapter = new LogbackMDCAdapter();

@BeforeEach
void setUp() {
loggerContext.setMDCAdapter(mdcAdapter);
}

@Test
public void standaloneEventEvaluatorTest() throws JoranException {
jc.setContext(loggerContext);
jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "simpleEvaluator.xml");
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_0);
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_1);
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_2);
//statusPrinter2.print(loggerContext);
}

@Test
public void eventEvaluatorEmbeddedInFilterTest() throws JoranException {
jc.setContext(loggerContext);
jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "basicEventEvaluator.xml");
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_0);
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_1);
statusChecker.assertContainsMatch(StubEventEvaluator.MSG_2);

Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);

ListAppender listAppender = (ListAppender) root.getAppender("LIST");
List<ILoggingEvent> eventList = listAppender.list;

String message = "hello";
Logger logger = loggerContext.getLogger(this.getClass());
logger.warn(message);
assertEquals(1, eventList.size());
assertEquals(message, eventList.get(0).getMessage());
statusPrinter2.print(loggerContext);
}

}

0 comments on commit cb60369

Please # to comment.