Skip to content

Commit

Permalink
migrate IncludeAction code to IncludeModelHandler, LOGBACK-1746
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Apr 10, 2024
1 parent b95d2a0 commit af92be4
Show file tree
Hide file tree
Showing 16 changed files with 433 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ch.qos.logback.classic.model.processor.ConfigurationModelHandlerFull;
import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.joran.GenericXMLConfigurator;
import ch.qos.logback.core.joran.JoranConfiguratorBase;
import ch.qos.logback.core.joran.action.AppenderRefAction;
import ch.qos.logback.core.joran.action.IncludeAction;
Expand All @@ -34,6 +35,7 @@
import ch.qos.logback.core.joran.spi.RuleStore;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.ModelInterpretationContext;

/**
* JoranConfigurator class adds rules specific to logback-classic.
Expand All @@ -42,6 +44,8 @@
*/
public class JoranConfigurator extends JoranConfiguratorBase<ILoggingEvent> {



@Override
public void addElementSelectorAndActionAssociations(RuleStore rs) {
// add parent rules
Expand Down Expand Up @@ -81,6 +85,17 @@ protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegi
LogbackClassicDefaultNestedComponentRules.addDefaultNestedComponentRegistryRules(registry);
}

private JoranConfigurator makeAnotherInstance() {
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(context);
return jc;
}

public void buildModelInterpretationContext() {
super.buildModelInterpretationContext();
this.modelInterpretationContext.setConfiguratorSupplier( () -> this.makeAnotherInstance() );
}

@Override
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
ModelClassToModelHandlerLinker m = new ModelClassToModelHandlerLinker(context);
Expand Down
3 changes: 3 additions & 0 deletions logback-classic/src/test/input/joran/include/included0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<included>
<root level="ERROR"/>
</included>
22 changes: 22 additions & 0 deletions logback-classic/src/test/input/joran/include/topLevel0.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


<!--
~ 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>

<variable name="JO_PREFIX" value="src/test/input/joran" />
<include file="${JO_PREFIX}/include/included0.xml"/>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@
public class LoggerContextDeadlockTest {

LoggerContext loggerContext = new LoggerContext();
JoranConfigurator jc = new JoranConfigurator();

GetLoggerThread getLoggerThread = new GetLoggerThread(loggerContext);

@BeforeEach
public void setUp() throws Exception {
jc.setContext(loggerContext);

}

@AfterEach
public void tearDown() throws Exception {
}

// LBCLASSIC_81
// LOGBACK-394
@Test
@Timeout(value = 20, unit= TimeUnit.SECONDS)
public void testLBCLASSIC_81() throws JoranException {
public void test_LOGBACK_394() throws JoranException {

getLoggerThread.start();
for (int i = 0; i < 500; i++) {
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(loggerContext);
ByteArrayInputStream baos = new ByteArrayInputStream(
"<configuration><root level=\"DEBUG\"/></configuration>".getBytes());
jc.doConfigure(baos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import ch.qos.logback.core.testUtil.StringListAppender;
import ch.qos.logback.core.util.CachingDateFormatter;
import ch.qos.logback.core.util.StatusPrinter;
import ch.qos.logback.core.util.StatusPrinter2;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.MDC;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class JoranConfiguratorTest {
LoggerContext loggerContext = new LoggerContext();
Logger logger = loggerContext.getLogger(this.getClass().getName());
Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
StatusPrinter2 statusPrinter2 = new StatusPrinter2();
StatusChecker checker = new StatusChecker(loggerContext);
int diff = RandomUtil.getPositiveInt();

Expand Down Expand Up @@ -669,7 +671,16 @@ public void kvp() throws JoranException {
assertTrue(slAppender.strList.get(2).contains("null=\"" + kvpNullKey.value + "\" " + msg));
assertTrue(slAppender.strList.get(3).contains(kvpNullValue.key + "=\"null\" " + msg));
}



@Test
public void inclusionWithVariables() throws JoranException {
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "include/topLevel0.xml");

Logger root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
statusPrinter2.print(loggerContext);
assertEquals(Level.ERROR, root.getLevel());
}

// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=46697
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public abstract class GenericXMLConfigurator extends ContextAwareBase {
public ModelInterpretationContext getModelInterpretationContext() {
return this.modelInterpretationContext;
}
private RuleStore ruleStore;

public final void doConfigure(URL url) throws JoranException {
InputStream in = null;
Expand Down Expand Up @@ -135,14 +136,21 @@ protected ElementPath initialElementPath() {
}

protected void buildSaxEventInterpreter(List<SaxEvent> saxEvents) {
RuleStore rs = new SimpleRuleStore(context);
RuleStore rs = getRuleStore();
addElementSelectorAndActionAssociations(rs);
this.saxEventInterpreter = new SaxEventInterpreter(context, rs, initialElementPath(), saxEvents);
SaxEventInterpretationContext interpretationContext = saxEventInterpreter.getSaxEventInterpretationContext();
interpretationContext.setContext(context);
setImplicitRuleSupplier(saxEventInterpreter);
}

public RuleStore getRuleStore() {
if(this.ruleStore == null) {
this.ruleStore = new SimpleRuleStore(context);
}
return this.ruleStore;
}

protected void buildModelInterpretationContext() {
this.modelInterpretationContext = new ModelInterpretationContext(context);
addDefaultNestedComponentRegistryRules(modelInterpretationContext.getDefaultNestedComponentRegistry());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {

defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);


defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
public abstract class JoranConstants {
public static final String INCLUDED_TAG = "included";
public static final String CONFIGURATION_TAG = "configuration";

public static final String INCLUDE_TAG = "include";

public static final String APPENDER_TAG = "appender";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import ch.qos.logback.core.model.processor.EventEvaluatorModelHandler;
import ch.qos.logback.core.model.processor.ImplicitModelHandler;
import ch.qos.logback.core.model.processor.ImportModelHandler;
import ch.qos.logback.core.model.processor.IncludeModelHandler;
import ch.qos.logback.core.model.processor.NOPModelHandler;
import ch.qos.logback.core.model.processor.PropertyModelHandler;
import ch.qos.logback.core.model.processor.SequenceNumberGeneratorModelHandler;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void link(DefaultProcessor defaultProcessor) {

defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, NOPModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);


defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);
Expand Down
Loading

0 comments on commit af92be4

Please sign in to comment.