Skip to content

Commit

Permalink
more work on model based configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Apr 9, 2019
1 parent 553b10c commit e9e4a27
Show file tree
Hide file tree
Showing 42 changed files with 1,698 additions and 686 deletions.
Expand Up @@ -17,7 +17,6 @@
import ch.qos.logback.access.PatternLayoutEncoder;
import ch.qos.logback.access.boolex.JaninoEventEvaluator;
import ch.qos.logback.access.joran.action.ConfigurationAction;
import ch.qos.logback.access.joran.action.EvaluatorAction;
import ch.qos.logback.access.spi.IAccessEvent;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
Expand Down Expand Up @@ -50,8 +49,6 @@ public void addInstanceRules(RuleStore rs) {

rs.addRule(new ElementSelector("configuration/appender/sift/*"), new NOPAction());

rs.addRule(new ElementSelector("configuration/evaluator"), new EvaluatorAction());

// add if-then-else support
rs.addRule(new ElementSelector("*/if"), new IfAction());
rs.addRule(new ElementSelector("*/if/then"), new ThenAction());
Expand Down
Expand Up @@ -14,11 +14,10 @@
package ch.qos.logback.access.joran.action;

import ch.qos.logback.access.boolex.JaninoEventEvaluator;
import ch.qos.logback.core.joran.action.AbstractEventEvaluatorAction;
import ch.qos.logback.core.joran.action.EventEvaluatorAction;

public class EvaluatorAction extends AbstractEventEvaluatorAction {
public class AccessEvaluatorAction extends EventEvaluatorAction {

@Override
protected String defaultClassName() {
return JaninoEventEvaluator.class.getName();
}
Expand Down
Expand Up @@ -16,7 +16,6 @@
import ch.qos.logback.classic.joran.action.ConfigurationAction;
import ch.qos.logback.classic.joran.action.ConsolePluginAction;
import ch.qos.logback.classic.joran.action.ContextNameAction;
import ch.qos.logback.classic.joran.action.EvaluatorAction;
import ch.qos.logback.classic.joran.action.InsertFromJNDIAction;
import ch.qos.logback.classic.joran.action.JMXConfiguratorAction;
import ch.qos.logback.classic.joran.action.LevelAction;
Expand Down Expand Up @@ -74,8 +73,6 @@ public void addInstanceRules(RuleStore rs) {
rs.addRule(new ElementSelector("configuration/contextName"), new ContextNameAction());
rs.addRule(new ElementSelector("configuration/contextListener"), new LoggerContextListenerAction());
rs.addRule(new ElementSelector("configuration/insertFromJNDI"), new InsertFromJNDIAction());
rs.addRule(new ElementSelector("configuration/evaluator"), new EvaluatorAction());


rs.addRule(new ElementSelector("configuration/logger"), new LoggerAction());
rs.addRule(new ElementSelector("configuration/logger/level"), new LevelAction());
Expand Down
Expand Up @@ -14,9 +14,9 @@
package ch.qos.logback.classic.joran.action;

import ch.qos.logback.classic.boolex.JaninoEventEvaluator;
import ch.qos.logback.core.joran.action.AbstractEventEvaluatorAction;
import ch.qos.logback.core.joran.action.EventEvaluatorAction;

public class EvaluatorAction extends AbstractEventEvaluatorAction {
public class ClassicEvaluatorAction extends EventEvaluatorAction {
protected String defaultClassName() {
return JaninoEventEvaluator.class.getName();
}
Expand Down
@@ -0,0 +1,67 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, 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.action;

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;

/**
* Action to handle the <level> element nested within <logger> element.
*
* <p>This action is <b>deprecated</b>. Use the level attribute within the logger
* element.
*
* @author Ceki Gulcu
*/
public class LevelActionOld extends Action {

boolean inError = false;

public void begin(InterpretationContext ec, String name, Attributes attributes) {
Object o = ec.peekObject();

if (!(o instanceof Logger)) {
inError = true;
addError("For element <level>, could not find a logger at the top of execution stack.");
return;
}

Logger l = (Logger) o;

String loggerName = l.getName();

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

if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
l.setLevel(null);
} else {
l.setLevel(Level.toLevel(levelStr, Level.DEBUG));
}

addInfo(loggerName + " level set to " + l.getLevel());
}

public void finish(InterpretationContext ec) {
}

public void end(InterpretationContext ec, String e) {
}
}
@@ -0,0 +1,93 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, 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.action;

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;

/**
* Action which handles <logger> elements in configuration files.
*
* @author Ceki Gulcu
*/
public class LoggerActionOld extends Action {
public static final String LEVEL_ATTRIBUTE = "level";

boolean inError = false;
Logger logger;

public void begin(InterpretationContext ec, String name, Attributes attributes) {
// Let us forget about previous errors (in this object)
inError = false;
logger = null;

LoggerContext loggerContext = (LoggerContext) this.context;

String loggerName = ec.subst(attributes.getValue(NAME_ATTRIBUTE));

if (OptionHelper.isEmpty(loggerName)) {
inError = true;
String aroundLine = getLineColStr(ec);
String errorMsg = "No 'name' attribute in element " + name + ", around " + aroundLine;
addError(errorMsg);
return;
}

logger = loggerContext.getLogger(loggerName);

String levelStr = ec.subst(attributes.getValue(LEVEL_ATTRIBUTE));

if (!OptionHelper.isEmpty(levelStr)) {
if (ActionConst.INHERITED.equalsIgnoreCase(levelStr) || ActionConst.NULL.equalsIgnoreCase(levelStr)) {
addInfo("Setting level of logger [" + loggerName + "] to null, i.e. INHERITED");
logger.setLevel(null);
} else {
Level level = Level.toLevel(levelStr);
addInfo("Setting level of logger [" + loggerName + "] to " + level);
logger.setLevel(level);
}
}

String additivityStr = ec.subst(attributes.getValue(ActionConst.ADDITIVITY_ATTRIBUTE));
if (!OptionHelper.isEmpty(additivityStr)) {
boolean additive = OptionHelper.toBoolean(additivityStr, true);
addInfo("Setting additivity of logger [" + loggerName + "] to " + additive);
logger.setAdditive(additive);
}
ec.pushObject(logger);
}

public void end(InterpretationContext ec, String e) {
if (inError) {
return;
}
Object o = ec.peekObject();
if (o != logger) {
addWarn("The object on the top the of the stack is not " + logger + " pushed earlier");
addWarn("It is: " + o);
} else {
ec.popObject();
}
}

public void finish(InterpretationContext ec) {
}
}
@@ -0,0 +1,78 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2015, 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.action;

import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.spi.ContextAware;
import ch.qos.logback.core.spi.LifeCycle;
import org.xml.sax.Attributes;

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

public class LoggerContextListenerActionOld extends Action {
boolean inError = false;
LoggerContextListener lcl;

@Override
public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {

inError = false;

String className = attributes.getValue(CLASS_ATTRIBUTE);
if (OptionHelper.isEmpty(className)) {
addError("Mandatory \"" + CLASS_ATTRIBUTE + "\" attribute not set for <contextListener> element");
inError = true;
return;
}

try {
lcl = (LoggerContextListener) OptionHelper.instantiateByClassName(className, LoggerContextListener.class, context);

if (lcl instanceof ContextAware) {
((ContextAware) lcl).setContext(context);
}

ec.pushObject(lcl);
addInfo("Adding LoggerContextListener of type [" + className + "] to the object stack");

} catch (Exception oops) {
inError = true;
addError("Could not create LoggerContextListener of type " + className + "].", oops);
}
}

@Override
public void end(InterpretationContext ec, String name) throws ActionException {
if (inError) {
return;
}
Object o = ec.peekObject();

if (o != lcl) {
addWarn("The object on the top the of the stack is not the LoggerContextListener pushed earlier.");
} else {
if (lcl instanceof LifeCycle) {
((LifeCycle) lcl).start();
addInfo("Starting LoggerContextListener");
}
((LoggerContext) context).addListener(lcl);
ec.popObject();
}
}

}
@@ -0,0 +1,19 @@
package ch.qos.logback.classic.model;

import ch.qos.logback.core.model.Model;

public class LevelModel extends Model {

String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}



}
@@ -0,0 +1,7 @@
package ch.qos.logback.classic.model;

import ch.qos.logback.core.model.ComponentModel;

public class LoggerContextListenerModel extends ComponentModel {

}
@@ -0,0 +1,35 @@
package ch.qos.logback.classic.model.processor;

import ch.qos.logback.classic.model.ContextNameModel;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.spi.InterpretationContext;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.processor.ModelHandlerBase;
import ch.qos.logback.core.model.processor.ModelHandlerException;

public class ContextNameModelHandler extends ModelHandlerBase {

public ContextNameModelHandler(Context context) {
super(context);
}

@Override
protected Class<ContextNameModel> getSupportedModelClass() {
return ContextNameModel.class;
}

@Override
public void handle(InterpretationContext intercon, Model model) throws ModelHandlerException {
ContextNameModel contextNameModel = (ContextNameModel) model;

String finalBody = intercon.subst(contextNameModel.getBodyText());
addInfo("Setting logger context name as [" + finalBody + "]");
try {
context.setName(finalBody);
} catch (IllegalStateException e) {
addError("Failed to rename context [" + context.getName() + "] as [" + finalBody + "]", e);
}

}

}

0 comments on commit e9e4a27

Please sign in to comment.