Skip to content

Commit

Permalink
support for inclusion via models
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Apr 16, 2019
1 parent 2ad7129 commit 4857232
Show file tree
Hide file tree
Showing 57 changed files with 1,073 additions and 634 deletions.
Expand Up @@ -26,7 +26,7 @@
import ch.qos.logback.core.filter.EvaluatorFilter;
import ch.qos.logback.core.joran.JoranConfiguratorBase;
import ch.qos.logback.core.joran.action.AppenderRefAction;
import ch.qos.logback.core.joran.action.IncludeAction;
import ch.qos.logback.core.joran.action.IncludeModelAction;
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.InterpretationContext;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void addInstanceRules(RuleStore rs) {
//rs.addRule(new ElementSelector("*/if/else"), new ElseAction());
//rs.addRule(new ElementSelector("*/if/else/*"), new NOPAction());

rs.addRule(new ElementSelector("configuration/include"), new IncludeAction());
rs.addRule(new ElementSelector("configuration/include"), new IncludeModelAction());
}

@Override
Expand Down
Expand Up @@ -38,15 +38,8 @@ public void handle(InterpretationContext intercon, Model model) throws ModelHand
StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
}

intercon.pushObject(context);
}

@Override
public void postHandle(InterpretationContext intercon, Model model) throws ModelHandlerException {
addInfo("End of configuration.");
intercon.popObject();
}

// public void begin(InterpretationContext ec, String name, Attributes attributes) {
//
// // See LBCLASSIC-225 (the system property is looked up first. Thus, it overrides
Expand Down
Expand Up @@ -41,16 +41,20 @@
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.JoranConfiguratorBase;
import ch.qos.logback.core.joran.action.AppenderRefAction;
import ch.qos.logback.core.joran.action.IncludeAction;
import ch.qos.logback.core.joran.action.IncludeModelAction;
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementSelector;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.joran.spi.RuleStore;
import ch.qos.logback.core.model.AppenderModel;
import ch.qos.logback.core.model.AppenderRefModel;
import ch.qos.logback.core.model.DefineModel;
import ch.qos.logback.core.model.IncludeModel;
import ch.qos.logback.core.model.processor.AppenderModelHandler;
import ch.qos.logback.core.model.processor.AppenderRefModelHandler;
import ch.qos.logback.core.model.processor.ChainedModelFilter;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.ModelFiler;

/**
* JoranConfigurator class adds rules specific to logback-classic.
Expand Down Expand Up @@ -91,7 +95,7 @@ public void addInstanceRules(RuleStore rs) {
if (PlatformInfo.hasJMXObjectName()) {
rs.addRule(new ElementSelector("configuration/jmxConfigurator"), new JMXConfiguratorAction());
}
rs.addRule(new ElementSelector("configuration/include"), new IncludeAction());
rs.addRule(new ElementSelector("configuration/include"), new IncludeModelAction());

rs.addRule(new ElementSelector("configuration/consolePlugin"), new ConsolePluginAction());

Expand All @@ -110,13 +114,19 @@ protected DefaultProcessor buildDefaultProcessor(Context context, Interpretation
defaultProcessor.addHandler(ConfigurationModel.class, ConfigurationModelHandler.class);
defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler.class);
defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler.class);

defaultProcessor.addHandler(IncludeModel.class, AppenderModelHandler.class);

defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler.class);
defaultProcessor.addHandler(AppenderRefModel.class, AppenderRefModelHandler.class);
defaultProcessor.addHandler(RootLoggerModel.class, RootLoggerModelHandler.class);
defaultProcessor.addHandler(LoggerModel.class, LoggerModelHandler.class);
defaultProcessor.addHandler(LevelModel.class, LevelModelHandler.class);


ModelFiler phaseOneFilter = new ChainedModelFilter().allow(ConfigurationModel.class).allow(DefineModel.class).denyAll();
ModelFiler phaseTwoFilter = new ChainedModelFilter().allowAll();
defaultProcessor.setPhaseOneFilter(phaseOneFilter);
defaultProcessor.setPhaseTwoFilter(phaseTwoFilter);
return defaultProcessor;
}

Expand Down
Expand Up @@ -151,7 +151,7 @@ private void fallbackConfiguration(LoggerContext lc, List<SaxEvent> eventList, U
try {
lc.reset();
ConfigurationWatchListUtil.registerConfigurationWatchList(context, newCWL);
joranConfigurator.doConfigure(failsafeEvents);
joranConfigurator.playSaxEvents(failsafeEvents);
addInfo(RE_REGISTERING_PREVIOUS_SAFE_CONFIGURATION);
joranConfigurator.registerSafeConfiguration(eventList);

Expand Down
Expand Up @@ -17,7 +17,7 @@

import ch.qos.logback.classic.model.LevelModel;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.JoranConstants;
import ch.qos.logback.core.joran.action.BaseModelAction;
import ch.qos.logback.core.joran.action.PreconditionValidator;
import ch.qos.logback.core.joran.spi.InterpretationContext;
Expand Down Expand Up @@ -49,7 +49,7 @@ protected boolean validPreconditions(InterpretationContext interpcont, String na
protected Model buildCurrentModel(InterpretationContext interpretationContext, String name, Attributes attributes) {
LevelModel lm = new LevelModel();

String value = attributes.getValue(ActionConst.VALUE_ATTR);
String value = attributes.getValue(JoranConstants.VALUE_ATTR);
lm.setValue(value);

return lm;
Expand Down
Expand Up @@ -13,12 +13,15 @@
*/
package ch.qos.logback.classic.joran.action;

import static ch.qos.logback.core.joran.JoranConstants.INHERITED;
import static ch.qos.logback.core.joran.JoranConstants.NULL;
import static ch.qos.logback.core.joran.JoranConstants.VALUE_ATTR;

import org.xml.sax.Attributes;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.spi.InterpretationContext;

/**
Expand Down Expand Up @@ -46,11 +49,11 @@ public void begin(InterpretationContext ec, String name, Attributes attributes)

String loggerName = l.getName();

String levelStr = ec.subst(attributes.getValue(ActionConst.VALUE_ATTR));
String levelStr = ec.subst(attributes.getValue(VALUE_ATTR));
// addInfo("Encapsulating logger name is [" + loggerName
// + "], level value is [" + levelStr + "].");

if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
if (INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
l.setLevel(null);
} else {
l.setLevel(Level.toLevel(levelStr, Level.DEBUG));
Expand Down
Expand Up @@ -16,7 +16,7 @@
import org.xml.sax.Attributes;

import ch.qos.logback.classic.model.LoggerModel;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.JoranConstants;
import ch.qos.logback.core.joran.action.BaseModelAction;
import ch.qos.logback.core.joran.action.PreconditionValidator;
import ch.qos.logback.core.joran.spi.InterpretationContext;
Expand Down Expand Up @@ -44,10 +44,10 @@ protected Model buildCurrentModel(InterpretationContext interpretationContext, S
String nameStr = attributes.getValue(NAME_ATTRIBUTE);
loggerModel.setName(nameStr);

String levelStr = attributes.getValue(ActionConst.LEVEL_ATTRIBUTE);
String levelStr = attributes.getValue(JoranConstants.LEVEL_ATTRIBUTE);
loggerModel.setLevel(levelStr);

String additivityStr = attributes.getValue(ActionConst.ADDITIVITY_ATTRIBUTE);
String additivityStr = attributes.getValue(JoranConstants.ADDITIVITY_ATTRIBUTE);
loggerModel.setAdditivity(additivityStr);

return loggerModel;
Expand Down
Expand Up @@ -13,13 +13,16 @@
*/
package ch.qos.logback.classic.joran.action;

import static ch.qos.logback.core.joran.JoranConstants.ADDITIVITY_ATTRIBUTE;
import static ch.qos.logback.core.joran.JoranConstants.INHERITED;
import static ch.qos.logback.core.joran.JoranConstants.NULL;

import org.xml.sax.Attributes;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.util.OptionHelper;

Expand Down Expand Up @@ -56,7 +59,7 @@ public void begin(InterpretationContext ec, String name, Attributes attributes)
String levelStr = ec.subst(attributes.getValue(LEVEL_ATTRIBUTE));

if (!OptionHelper.isEmpty(levelStr)) {
if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
if (INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
addInfo("Setting level of logger [" + loggerName + "] to null, i.e. INHERITED");
logger.setLevel(null);
} else {
Expand All @@ -66,7 +69,7 @@ public void begin(InterpretationContext ec, String name, Attributes attributes)
}
}

String additivityStr = ec.subst(attributes.getValue(ActionConst.ADDITIVITY_ATTRIBUTE));
String additivityStr = ec.subst(attributes.getValue(ADDITIVITY_ATTRIBUTE));
if (!OptionHelper.isEmpty(additivityStr)) {
boolean additive = OptionHelper.toBoolean(additivityStr, true);
addInfo("Setting additivity of logger [" + loggerName + "] to " + additive);
Expand Down
Expand Up @@ -17,7 +17,7 @@

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.model.RootLoggerModel;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.JoranConstants;
import ch.qos.logback.core.joran.action.BaseModelAction;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.model.Model;
Expand All @@ -31,7 +31,7 @@ public class RootLoggerAction extends BaseModelAction {
protected Model buildCurrentModel(InterpretationContext interpretationContext, String name, Attributes attributes) {
RootLoggerModel rootLoggerModel = new RootLoggerModel();

String levelStr = attributes.getValue(ActionConst.LEVEL_ATTRIBUTE);
String levelStr = attributes.getValue(JoranConstants.LEVEL_ATTRIBUTE);
rootLoggerModel.setLevel(levelStr);

return rootLoggerModel;
Expand Down
Expand Up @@ -13,13 +13,14 @@
*/
package ch.qos.logback.classic.joran.action;

import static ch.qos.logback.core.joran.JoranConstants.LEVEL_ATTRIBUTE;

import org.xml.sax.Attributes;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.action.Action;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.util.OptionHelper;

Expand All @@ -34,7 +35,7 @@ public void begin(InterpretationContext ec, String name, Attributes attributes)
LoggerContext loggerContext = (LoggerContext) this.context;
root = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);

String levelStr = ec.subst(attributes.getValue(ActionConst.LEVEL_ATTRIBUTE));
String levelStr = ec.subst(attributes.getValue(LEVEL_ATTRIBUTE));
if (!OptionHelper.isEmpty(levelStr)) {
Level level = Level.toLevel(levelStr);
addInfo("Setting level of ROOT logger to " + level);
Expand Down
@@ -1,10 +1,12 @@
package ch.qos.logback.classic.model.processor;

import static ch.qos.logback.core.joran.JoranConstants.INHERITED;
import static ch.qos.logback.core.joran.JoranConstants.NULL;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.model.LevelModel;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.processor.ModelHandlerBase;
Expand Down Expand Up @@ -39,7 +41,7 @@ public void handle(InterpretationContext intercon, Model model) throws ModelHand

LevelModel levelModel = (LevelModel) model;
String levelStr = intercon.subst(levelModel.getValue());
if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
if (INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
l.setLevel(null);
} else {
l.setLevel(Level.toLevel(levelStr, Level.DEBUG));
Expand Down
@@ -1,11 +1,13 @@
package ch.qos.logback.classic.model.processor;

import static ch.qos.logback.core.joran.JoranConstants.NULL;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.model.LoggerModel;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.action.ActionConst;
import ch.qos.logback.core.joran.JoranConstants;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.processor.ModelHandlerBase;
Expand Down Expand Up @@ -39,7 +41,7 @@ public void handle(InterpretationContext intercon, Model model) throws ModelHand

String levelStr = intercon.subst(loggerModel.getLevel());
if (!OptionHelper.isEmpty(levelStr)) {
if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
if (JoranConstants.INHERITED.equalsIgnoreCase(levelStr) || NULL.equalsIgnoreCase(levelStr)) {
addInfo("Setting level of logger [" + finalLoggerName + "] to null, i.e. INHERITED");
logger.setLevel(null);
} else {
Expand Down
Expand Up @@ -226,7 +226,7 @@ private void fallbackConfiguration(LoggerContext lc, List<SaxEvent> eventList, U
try {
lc.reset();
JoranConfigurator.informContextOfURLUsedForConfiguration(context, mainURL);
joranConfigurator.doConfigure(eventList);
joranConfigurator.playSaxEvents(eventList);
addInfo("Re-registering previous fallback configuration once more as a fallback configuration point");
joranConfigurator.registerSafeConfiguration(eventList);
} catch (JoranException e) {
Expand Down
Expand Up @@ -34,7 +34,6 @@
import ch.qos.logback.core.boolex.EvaluationException;
import ch.qos.logback.core.boolex.EventEvaluator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

public class EvaluatorJoranTest {

Expand Down
Expand Up @@ -31,7 +31,7 @@
import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry;
import ch.qos.logback.core.joran.spi.ElementPath;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.joran.spi.Interpreter;
import ch.qos.logback.core.joran.spi.SaxEventInterpreter;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.joran.spi.RuleStore;
import ch.qos.logback.core.joran.spi.SimpleRuleStore;
Expand All @@ -43,7 +43,7 @@

public abstract class GenericConfigurator extends ContextAwareBase {

protected Interpreter interpreter;
protected SaxEventInterpreter interpreter;

public final void doConfigure(URL url) throws JoranException {
InputStream in = null;
Expand Down Expand Up @@ -117,7 +117,7 @@ public final void doConfigure(InputStream inputStream, String systemId) throws J

protected abstract void addInstanceRules(RuleStore rs);

protected abstract void addImplicitRules(Interpreter interpreter);
protected abstract void addImplicitRules(SaxEventInterpreter interpreter);

protected void addDefaultNestedComponentRegistryRules(DefaultNestedComponentRegistry registry) {

Expand All @@ -130,7 +130,7 @@ protected ElementPath initialElementPath() {
protected void buildInterpreter() {
RuleStore rs = new SimpleRuleStore(context);
addInstanceRules(rs);
this.interpreter = new Interpreter(context, rs, initialElementPath());
this.interpreter = new SaxEventInterpreter(context, rs, initialElementPath());
InterpretationContext interpretationContext = interpreter.getInterpretationContext();
interpretationContext.setContext(context);
addImplicitRules(interpreter);
Expand All @@ -147,7 +147,13 @@ public final void doConfigure(final InputSource inputSource) throws JoranExcepti
// }
SaxEventRecorder recorder = new SaxEventRecorder(context);
recorder.recordEvents(inputSource);
doConfigure(recorder.saxEventList);
buildInterpreter();

playSaxEvents(recorder.saxEventList);

Model top = interpreter.getInterpretationContext().peekModel();
processModel(top);

// no exceptions a this level
StatusUtil statusUtil = new StatusUtil(context);
if (statusUtil.noXMLParsingErrorsOccurred(threshold)) {
Expand All @@ -156,18 +162,15 @@ public final void doConfigure(final InputSource inputSource) throws JoranExcepti
}
}

public void doConfigure(final List<SaxEvent> eventList) throws JoranException {
buildInterpreter();
public void playSaxEvents(final List<SaxEvent> eventList) throws JoranException {
// disallow simultaneous configurations of the same context
synchronized (context.getConfigurationLock()) {
interpreter.getEventPlayer().play(eventList);
}

Model top = interpreter.getInterpretationContext().peekModel();
doConfigure(top);
}

public void doConfigure(Model model) {
protected void processModel(Model model) {
DefaultProcessor defaultProcessor = buildDefaultProcessor(context, interpreter.getInterpretationContext());
defaultProcessor.process(model);
}
Expand Down

0 comments on commit 4857232

Please sign in to comment.