Skip to content

Commit

Permalink
further progress on PropertyConfigurator
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Sep 4, 2024
1 parent 3007edf commit 9f4a2bc
Show file tree
Hide file tree
Showing 15 changed files with 498 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@
*/
package ch.qos.logback.classic.joran;

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.InsertFromJNDIAction;
import ch.qos.logback.classic.joran.action.LevelAction;
import ch.qos.logback.classic.joran.action.LoggerAction;
import ch.qos.logback.classic.joran.action.LoggerContextListenerAction;
import ch.qos.logback.classic.joran.action.ReceiverAction;
import ch.qos.logback.classic.joran.action.RootLoggerAction;
import ch.qos.logback.classic.joran.action.*;
import ch.qos.logback.classic.joran.sanity.IfNestedWithinSecondPhaseElementSC;
import ch.qos.logback.classic.model.processor.ConfigurationModelHandlerFull;
import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
Expand Down Expand Up @@ -66,11 +58,12 @@ public void addElementSelectorAndActionAssociations(RuleStore rs) {
rs.addRule(new ElementSelector("configuration/root/appender-ref"), () -> new AppenderRefAction());

rs.addRule(new ElementSelector("configuration/include"), () -> new IncludeAction());

rs.addRule(new ElementSelector("configuration/propertyConfigurator"), () -> new PropertyConfiguratorAction());
rs.addRule(new ElementSelector("configuration/consolePlugin"), () -> new ConsolePluginAction());

rs.addRule(new ElementSelector("configuration/receiver"), () -> new ReceiverAction());


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,8 @@

package ch.qos.logback.classic.joran;

import ch.qos.logback.classic.model.ConfigurationModel;
import ch.qos.logback.classic.model.ContextNameModel;
import ch.qos.logback.classic.model.LevelModel;
import ch.qos.logback.classic.model.LoggerContextListenerModel;
import ch.qos.logback.classic.model.LoggerModel;
import ch.qos.logback.classic.model.RootLoggerModel;
import ch.qos.logback.classic.model.processor.ConfigurationModelHandler;
import ch.qos.logback.classic.model.processor.ContextNameModelHandler;
import ch.qos.logback.classic.model.processor.LevelModelHandler;
import ch.qos.logback.classic.model.processor.LoggerContextListenerModelHandler;
import ch.qos.logback.classic.model.processor.LoggerModelHandler;
import ch.qos.logback.classic.model.processor.RootLoggerModelHandler;
import ch.qos.logback.classic.model.*;
import ch.qos.logback.classic.model.processor.*;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.ModelClassToModelHandlerLinkerBase;
import ch.qos.logback.core.model.AppenderModel;
Expand Down Expand Up @@ -63,6 +53,7 @@ public void link(DefaultProcessor defaultProcessor) {
defaultProcessor.addHandler(ContextNameModel.class, ContextNameModelHandler::makeInstance);
defaultProcessor.addHandler(LoggerContextListenerModel.class, LoggerContextListenerModelHandler::makeInstance);

defaultProcessor.addHandler(PropertyConfiguratorModel.class, PropertyConfiguratorModelHandler::makeInstance);
defaultProcessor.addHandler(InsertFromJNDIModel.class, InsertFromJNDIModelHandler::makeInstance);

defaultProcessor.addHandler(AppenderModel.class, AppenderModelHandler::makeInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void setContext(Context context) {
super.setContext(context);
}

void doConfigure(URL url) throws JoranException {
public void doConfigure(URL url) throws JoranException {
try {
URLConnection urlConnection = url.openConnection();
// per http://jira.qos.ch/browse/LOGBACK-117
Expand All @@ -76,15 +76,15 @@ void doConfigure(URL url) throws JoranException {
}
}

void doConfigure(String filename) throws JoranException {
public void doConfigure(String filename) throws JoranException {
try(FileInputStream fileInputStream = new FileInputStream(filename)) {
doConfigure(fileInputStream);
} catch (IOException e) {
throw new JoranException("Failed to load file "+filename, e);
}
}

void doConfigure(InputStream inputStream) throws JoranException {
public void doConfigure(InputStream inputStream) throws JoranException {
Properties props = new Properties();
try {
props.load(inputStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package ch.qos.logback.classic.joran.action;

import ch.qos.logback.classic.model.PropertyConfiguratorModel;
import ch.qos.logback.core.joran.action.ResourceAction;
import ch.qos.logback.core.model.IncludeModel;

/**
* Build an {@link PropertyConfiguratorModel} instance from SAX events.
*
* @author Ceki G&uuml;lc&uuml;
* @since 1.5.8
*/
public class PropertyConfiguratorAction extends ResourceAction {

protected PropertyConfiguratorModel makeNewResourceModel() {
return new PropertyConfiguratorModel();
}

}
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.
*/

package ch.qos.logback.classic.model;

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

public class PropertyConfiguratorModel extends ResourceModel {

private static final long serialVersionUID = -2009536798661734346L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*/

package ch.qos.logback.classic.model.processor;

import ch.qos.logback.classic.joran.PropertyConfigurator;
import ch.qos.logback.classic.model.PropertyConfiguratorModel;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
import ch.qos.logback.core.model.IncludeModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.model.ResourceModel;
import ch.qos.logback.core.model.processor.ModelHandlerException;
import ch.qos.logback.core.model.processor.ModelInterpretationContext;
import ch.qos.logback.core.model.processor.ResourceHandlerBase;
import ch.qos.logback.core.util.OptionHelper;

import java.io.InputStream;
import java.net.URL;

public class PropertyConfiguratorModelHandler extends ResourceHandlerBase {
boolean inError = false;

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

static public PropertyConfiguratorModelHandler makeInstance(Context context, ModelInterpretationContext mic) {
return new PropertyConfiguratorModelHandler(context);
}

@Override
public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
PropertyConfiguratorModel propertyConfiguratorModel = (PropertyConfiguratorModel) model;

this.optional = OptionHelper.toBoolean(propertyConfiguratorModel.getOptional(), false);

if (!checkAttributes(propertyConfiguratorModel)) {
inError = true;
return;
}

InputStream in = getInputStream(mic, propertyConfiguratorModel);
if(in == null) {
inError = true;
return;
}

addInfo("Reading configuration from ["+getAttribureInUse()+"]");

PropertyConfigurator propertyConfigurator = new PropertyConfigurator();
propertyConfigurator.setContext(mic.getContext());
try {
propertyConfigurator.doConfigure(in);
} catch (JoranException e) {
addError("Could not configure from "+getAttribureInUse());
throw new ModelHandlerException(e);
}

}

protected InputStream getInputStream(ModelInterpretationContext mic, ResourceModel resourceModel) {
URL inputURL = getInputURL(mic, resourceModel);
if (inputURL == null)
return null;

ConfigurationWatchListUtil.addToWatchList(context, inputURL);
return openURL(inputURL);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# 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.
#

logback.logger.com.toto = WARN
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
~ 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>

<appender name="LIST" class="ch.qos.logback.core.testUtil.StringListAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%msg</Pattern>
</layout>
</appender>

<variable name="JO_PREFIX" value="src/test/input/joran" />

<propertyConfigurator file="${JO_PREFIX}/propertyConfigurator/smoke.properties" />

<root level="debug">
<appender-ref ref="LIST" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ public void dateConverterWithLocale() throws JoranException {
//StatusPrinter.print(loggerContext);
}

@Test
public void propertyConfiguratorSmoke() throws JoranException {
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "propertyConfigurator/smoke.xml");
Logger com_foo_Logger = loggerContext.getLogger("com.toto");
StatusPrinter.print(loggerContext);
assertEquals(Level.WARN, com_foo_Logger.getLevel());


}

@Test
public void consoleCharsetTest() throws JoranException {
if (EnvUtil.isJDK21OrHigher()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,91 +13,19 @@
*/
package ch.qos.logback.core.joran.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.List;

import org.xml.sax.Attributes;

import ch.qos.logback.core.joran.event.SaxEvent;
import ch.qos.logback.core.joran.event.SaxEventRecorder;
import ch.qos.logback.core.joran.spi.ActionException;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.joran.spi.SaxEventInterpretationContext;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
import ch.qos.logback.core.model.IncludeModel;
import ch.qos.logback.core.model.Model;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.OptionHelper;

import static ch.qos.logback.core.joran.JoranConstants.INCLUDED_TAG;
import ch.qos.logback.core.model.ResourceModel;

/**
*
* @author ceki
* Build an {@link IncludeModel} instance from SAX events.
*
* @author Ceki G&uuml;lc&uuml;
*
*/
public class IncludeAction extends Action {

private static final String FILE_ATTR = "file";
private static final String URL_ATTR = "url";
private static final String RESOURCE_ATTR = "resource";
private static final String OPTIONAL_ATTR = "optional";

Model parentModel;
IncludeModel includeModel;
boolean inError = false;

@Override
public void begin(SaxEventInterpretationContext seic, String tagName, Attributes attributes) throws ActionException {

String optionalStr = attributes.getValue(OPTIONAL_ATTR);
public class IncludeAction extends ResourceAction {

this.includeModel = new IncludeModel();
this.includeModel.setOptional(optionalStr);
fillInIncludeModelAttributes(includeModel, tagName, attributes);
if (!seic.isModelStackEmpty()) {
parentModel = seic.peekModel();
}
final int lineNumber = getLineNumber(seic);
this.includeModel.setLineNumber(lineNumber);
seic.pushModel(this.includeModel);
protected ResourceModel makeNewResourceModel() {
return new IncludeModel();
}

private void fillInIncludeModelAttributes(IncludeModel includeModel, String tagName, Attributes attributes) {
this.includeModel.setTag(tagName);
String fileAttribute = attributes.getValue(FILE_ATTR);
String urlAttribute = attributes.getValue(URL_ATTR);
String resourceAttribute = attributes.getValue(RESOURCE_ATTR);

this.includeModel.setFile(fileAttribute);
this.includeModel.setUrl(urlAttribute);
this.includeModel.setResource(resourceAttribute);
}


@Override
public void end(SaxEventInterpretationContext seic, String name) throws ActionException {

if(inError)
return;

Model m = seic.peekModel();

if (m != includeModel) {
addWarn("The object at the of the stack is not the model [" + includeModel.idString()
+ "] pushed earlier.");
addWarn("This is wholly unexpected.");
}

// do not pop nor add to parent if there is no parent
if (parentModel != null) {
parentModel.addSubModel(includeModel);
seic.popModel();
}
}
}
Loading

0 comments on commit 9f4a2bc

Please sign in to comment.