diff --git a/logback-classic-blackbox/src/test/blackboxInput/joran/callerData.xml b/logback-classic-blackbox/src/test/blackboxInput/joran/callerData.xml index 560c5f4b94..81a93042d4 100644 --- a/logback-classic-blackbox/src/test/blackboxInput/joran/callerData.xml +++ b/logback-classic-blackbox/src/test/blackboxInput/joran/callerData.xml @@ -2,18 +2,14 @@ + + - - m.matches(message) - - m - ^hello.* - false - + + hello - + %caller{4, helloEval}%d %level - %m%n diff --git a/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/evaluator/MatchHelloEvaluator.java b/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/evaluator/MatchHelloEvaluator.java new file mode 100644 index 0000000000..0ab852d66e --- /dev/null +++ b/logback-classic-blackbox/src/test/java/ch/qos/logback/classic/blackbox/evaluator/MatchHelloEvaluator.java @@ -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 { + + 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; + } + +} diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/boolex/StubEventEvaluator.java b/logback-classic/src/main/java/ch/qos/logback/classic/boolex/StubEventEvaluator.java new file mode 100644 index 0000000000..0e7bb61fe8 --- /dev/null +++ b/logback-classic/src/main/java/ch/qos/logback/classic/boolex/StubEventEvaluator.java @@ -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 { + + 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 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); + } + +} diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/LogbackClassicDefaultNestedComponentRules.java b/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/LogbackClassicDefaultNestedComponentRules.java index f9d034e579..862603d774 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/LogbackClassicDefaultNestedComponentRules.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/model/processor/LogbackClassicDefaultNestedComponentRules.java @@ -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; @@ -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); } @@ -63,8 +65,7 @@ public static List 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; diff --git a/logback-classic/src/test/input/joran/basicEventEvaluator.xml b/logback-classic/src/test/input/joran/basicEventEvaluator.xml new file mode 100644 index 0000000000..52177d7b53 --- /dev/null +++ b/logback-classic/src/test/input/joran/basicEventEvaluator.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + message.contains("billing") + + NEUTRAL + DENY + + + %-4relative [%thread] %-5level %logger - %msg%n + + + + + + + \ No newline at end of file diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/joran/sanity/EvaluatorStubTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/joran/sanity/EvaluatorStubTest.java new file mode 100644 index 0000000000..36110f27ec --- /dev/null +++ b/logback-classic/src/test/java/ch/qos/logback/classic/joran/sanity/EvaluatorStubTest.java @@ -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 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); + } + +}