diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java b/logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java index b5a6e538c3..da474f8370 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java @@ -17,29 +17,37 @@ import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.util.LevelUtil; +import ch.qos.logback.core.Context; +import ch.qos.logback.core.model.util.PropertyModelHandlerHelper; import ch.qos.logback.core.model.util.VariableSubstitutionsHelper; import ch.qos.logback.core.spi.ContextAwareBase; -import ch.qos.logback.core.spi.PropertyContainer; -import ch.qos.logback.core.spi.ScanException; +import ch.qos.logback.core.spi.ContextAwarePropertyContainer; import ch.qos.logback.core.status.OnConsoleStatusListener; import ch.qos.logback.core.util.OptionHelper; import ch.qos.logback.core.util.StatusListenerConfigHelper; import ch.qos.logback.core.util.StringUtil; -import java.util.HashMap; import java.util.Map; -public class TylerConfiguratorBase extends ContextAwareBase implements PropertyContainer { +public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer { - public static final String SET_CONTEXT_NAME = "setContextName"; + public static final String SET_CONTEXT_METHOD_NAME = "setContext"; + public static final String SET_CONTEXT_NAME_METHOD_NAME = "setContextName"; public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger"; + public static final String VARIABLE_SUBSTITUTIONS_HELPER_FIELD_NAME = "variableSubstitutionsHelper"; + public static final String PROPERTY_MODEL_HANDLER_HELPER_FIELD_NAME = "propertyModelHandlerHelper"; - VariableSubstitutionsHelper variableSubstitutionsHelper; + // initialized via #setContext + protected VariableSubstitutionsHelper variableSubstitutionsHelper; + // context set in #setContext + protected PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this); - private Logger setupLogger(String loggerName, Level level, String levelString, Boolean additivity) { + protected Logger setupLogger(String loggerName, String levelString, Boolean additivity) { LoggerContext loggerContext = (LoggerContext) context; Logger logger = loggerContext.getLogger(loggerName); if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) { + Level level = LevelUtil.levelStringToLevel(levelString); logger.setLevel(level); } if (additivity != null) { @@ -48,6 +56,13 @@ private Logger setupLogger(String loggerName, Level level, String levelString, B return logger; } + @Override + public void setContext(Context context) { + super.setContext(context); + variableSubstitutionsHelper = new VariableSubstitutionsHelper(context); + propertyModelHandlerHelper.setContext(context); + } + protected void setContextName(String name) { if(StringUtil.isNullOrEmpty(name)) { addError("Cannot set context name to null or empty string"); @@ -72,17 +87,9 @@ protected void addOnConsoleStatusListener() { * @param ref * @return */ + @Override public String subst(String ref) { - if (ref == null) { - return null; - } - - try { - return OptionHelper.substVars(ref, this, context); - } catch (ScanException | IllegalArgumentException e) { - addError("Problem while parsing [" + ref + "]", e); - return ref; - } + return variableSubstitutionsHelper.subst(ref); } @Override diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/tyler/VariableModelHelper.java b/logback-classic/src/main/java/ch/qos/logback/classic/tyler/VariableModelHelper.java index c2fa8b5887..543518413b 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/tyler/VariableModelHelper.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/tyler/VariableModelHelper.java @@ -18,7 +18,7 @@ import ch.qos.logback.core.joran.action.ActionUtil; import ch.qos.logback.core.model.ModelConstants; import ch.qos.logback.core.model.PropertyModel; -import ch.qos.logback.core.model.util.PropertyModelUtil; +import ch.qos.logback.core.model.util.PropertyModelHandlerHelper; import ch.qos.logback.core.model.util.VariableSubstitutionsHelper; import ch.qos.logback.core.spi.ContextAwareBase; import ch.qos.logback.core.util.ContextUtil; @@ -47,7 +47,7 @@ public class VariableModelHelper extends ContextAwareBase { void updateProperties(PropertyModel propertyModel) { ActionUtil.Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr()); - if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) { + if (PropertyModelHandlerHelper.checkFileAttributeSanity(propertyModel)) { String file = propertyModel.getFile(); file = tylerConfiguratorBase.subst(file); try (FileInputStream istream = new FileInputStream(file)) { @@ -58,7 +58,7 @@ void updateProperties(PropertyModel propertyModel) { // is badly malformed, i.e a binary. addError("Could not read properties file [" + file + "].", e1); } - } else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) { + } else if (PropertyModelHandlerHelper.checkResourceAttributeSanity(propertyModel)) { String resource = propertyModel.getResource(); resource = tylerConfiguratorBase.subst(resource); URL resourceURL = Loader.getResourceBySelfClassLoader(resource); @@ -71,7 +71,7 @@ void updateProperties(PropertyModel propertyModel) { addError("Could not read resource file [" + resource + "].", e); } } - } else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) { + } else if (PropertyModelHandlerHelper.checkValueNameAttributesSanity(propertyModel)) { // earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no // need to do this. See RegularEscapeUtil.__UNUSED__basicEscape String value = propertyModel.getValue(); diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/action/ActionUtil.java b/logback-core/src/main/java/ch/qos/logback/core/joran/action/ActionUtil.java index 78bc52b8ba..726bc0745c 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/joran/action/ActionUtil.java +++ b/logback-core/src/main/java/ch/qos/logback/core/joran/action/ActionUtil.java @@ -14,6 +14,7 @@ package ch.qos.logback.core.joran.action; import ch.qos.logback.core.model.processor.ModelInterpretationContext; +import ch.qos.logback.core.spi.ContextAwarePropertyContainer; import ch.qos.logback.core.util.OptionHelper; public class ActionUtil { @@ -50,7 +51,7 @@ static public Scope stringToScope(String scopeStr) { // } // } - static public void setProperty(ModelInterpretationContext ic, String key, String value, Scope scope) { + static public void setProperty(ContextAwarePropertyContainer ic, String key, String value, Scope scope) { switch (scope) { case LOCAL: ic.addSubstitutionProperty(key, value); diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/action/PropertyAction.java b/logback-core/src/main/java/ch/qos/logback/core/joran/action/PropertyAction.java index c050fc37a1..a5e1bc6148 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/joran/action/PropertyAction.java +++ b/logback-core/src/main/java/ch/qos/logback/core/joran/action/PropertyAction.java @@ -44,9 +44,9 @@ protected Model buildCurrentModel(SaxEventInterpretationContext interpretationCo PropertyModel propertyModel = new PropertyModel(); propertyModel.setName(attributes.getValue(NAME_ATTRIBUTE)); propertyModel.setValue(attributes.getValue(VALUE_ATTRIBUTE)); - propertyModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE)); propertyModel.setFile(attributes.getValue(FILE_ATTRIBUTE)); propertyModel.setResource(attributes.getValue(RESOURCE_ATTRIBUTE)); + propertyModel.setScopeStr(attributes.getValue(SCOPE_ATTRIBUTE)); return propertyModel; } diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java b/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java index d82b0c7551..24a8740d31 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/processor/InsertFromJNDIModelHandler.java @@ -7,7 +7,7 @@ import ch.qos.logback.core.joran.action.ActionUtil.Scope; import ch.qos.logback.core.model.InsertFromJNDIModel; import ch.qos.logback.core.model.Model; -import ch.qos.logback.core.model.util.PropertyModelUtil; +import ch.qos.logback.core.model.util.PropertyModelHandlerHelper; import ch.qos.logback.core.util.JNDIUtil; import ch.qos.logback.core.util.OptionHelper; @@ -61,7 +61,7 @@ public void handle(ModelInterpretationContext mic, Model model) throws ModelHand addError("[" + envEntryName + "] has null or empty value"); } else { addInfo("Setting variable [" + asKey + "] to [" + envEntryValue + "] in [" + scope + "] scope"); - PropertyModelUtil.setProperty(mic, asKey, envEntryValue, scope); + PropertyModelHandlerHelper.setProperty(mic, asKey, envEntryValue, scope); } } catch (NamingException e) { addError("Failed to lookup JNDI env-entry [" + envEntryName + "]"); diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java b/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java index 2877320568..64906fb4c8 100755 --- a/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/processor/PropertyModelHandler.java @@ -5,7 +5,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.util.Properties; import ch.qos.logback.core.Context; import ch.qos.logback.core.joran.action.ActionUtil; @@ -13,7 +12,7 @@ import ch.qos.logback.core.model.Model; import ch.qos.logback.core.model.ModelConstants; import ch.qos.logback.core.model.PropertyModel; -import ch.qos.logback.core.model.util.PropertyModelUtil; +import ch.qos.logback.core.model.util.PropertyModelHandlerHelper; import ch.qos.logback.core.util.Loader; public class PropertyModelHandler extends ModelHandlerBase { @@ -32,55 +31,12 @@ protected Class getSupportedModelClass() { } @Override - public void handle(ModelInterpretationContext interpretationContext, Model model) { + public void handle(ModelInterpretationContext mic, Model model) { PropertyModel propertyModel = (PropertyModel) model; - - Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr()); - - if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) { - String file = propertyModel.getFile(); - file = interpretationContext.subst(file); - try (FileInputStream istream = new FileInputStream(file)) { - loadAndSetProperties(interpretationContext, istream, scope); - } catch (FileNotFoundException e) { - addError("Could not find properties file [" + file + "]."); - } catch (IOException|IllegalArgumentException e1) { // IllegalArgumentException is thrown in case the file - // is badly malformed, i.e a binary. - addError("Could not read properties file [" + file + "].", e1); - } - } else if (PropertyModelUtil.checkResourceAttributeSanity(propertyModel)) { - String resource = propertyModel.getResource(); - resource = interpretationContext.subst(resource); - URL resourceURL = Loader.getResourceBySelfClassLoader(resource); - if (resourceURL == null) { - addError("Could not find resource [" + resource + "]."); - } else { - try ( InputStream istream = resourceURL.openStream();) { - loadAndSetProperties(interpretationContext, istream, scope); - } catch (IOException e) { - addError("Could not read resource file [" + resource + "].", e); - } - } - } else if (PropertyModelUtil.checkValueNameAttributesSanity(propertyModel)) { - // earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no - // need to do this. See RegularEscapeUtil.__UNUSED__basicEscape - String value = propertyModel.getValue(); - - // now remove both leading and trailing spaces - value = value.trim(); - value = interpretationContext.subst(value); - ActionUtil.setProperty(interpretationContext, propertyModel.getName(), value, scope); - - } else { - addError(ModelConstants.INVALID_ATTRIBUTES); - } - } - - void loadAndSetProperties(ModelInterpretationContext mic, InputStream istream, Scope scope) throws IOException { - Properties props = new Properties(); - props.load(istream); - PropertyModelUtil.setProperties(mic, props, scope); + PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this); + propertyModelHandlerHelper.setContext(context); + propertyModelHandlerHelper.handlePropertyModel(mic, propertyModel); } } diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelHandlerHelper.java b/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelHandlerHelper.java new file mode 100644 index 0000000000..6fb5e70e37 --- /dev/null +++ b/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelHandlerHelper.java @@ -0,0 +1,191 @@ +/* + * 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.core.model.util; + +import ch.qos.logback.core.joran.action.ActionUtil; +import ch.qos.logback.core.model.ModelConstants; +import ch.qos.logback.core.model.PropertyModel; +import ch.qos.logback.core.spi.ContextAware; +import ch.qos.logback.core.spi.ContextAwareBase; +import ch.qos.logback.core.spi.ContextAwarePropertyContainer; +import ch.qos.logback.core.spi.PropertyContainer; +import ch.qos.logback.core.util.ContextUtil; +import ch.qos.logback.core.util.Loader; +import ch.qos.logback.core.util.OptionHelper; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Properties; + +/** + * Given a {@link PropertyModel} offers methods to inject properties into a {@link PropertyContainer}. + * + * @since 1.5.1 + */ +public class PropertyModelHandlerHelper extends ContextAwareBase { + + public static final String HANDLE_PROPERTY_MODEL_METHOD_NAME = "handlePropertyModel"; + + public PropertyModelHandlerHelper(ContextAware declaredOrigin) { + super(declaredOrigin); + } + + /** + * Given a {@link PropertyModel} inject relevant properties into the given {@link ContextAwarePropertyContainer} + * parameter. + * + * @param capcm + * @param nameStr + * @param valueStr + * @param fileStr + * @param resourceStr + * @param scopeStr + * + */ + public void handlePropertyModel(ContextAwarePropertyContainer capcm, String nameStr, String valueStr, + String fileStr, String resourceStr, String scopeStr) { + PropertyModel propertyModel = new PropertyModel(); + propertyModel.setName(nameStr); + propertyModel.setValue(valueStr); + propertyModel.setFile(fileStr); + propertyModel.setResource(resourceStr); + + propertyModel.setScopeStr(scopeStr); + + handlePropertyModel(capcm, propertyModel); + } + + /** + * Given a {@link PropertyModel} inject relevant properties into the given {@link ContextAwarePropertyContainer} + * parameter. + * + * @param capc + * @param propertyModel + */ + public void handlePropertyModel(ContextAwarePropertyContainer capc, PropertyModel propertyModel) { + ActionUtil.Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr()); + + if (checkFileAttributeSanity(propertyModel)) { + String file = propertyModel.getFile(); + file = capc.subst(file); + try (FileInputStream istream = new FileInputStream(file)) { + PropertyModelHandlerHelper.loadAndSetProperties(capc, istream, scope); + } catch (FileNotFoundException e) { + addError("Could not find properties file [" + file + "]."); + } catch (IOException | IllegalArgumentException e1) { // IllegalArgumentException is thrown in case the file + // is badly malformed, i.e a binary. + addError("Could not read properties file [" + file + "].", e1); + } + } else if (checkResourceAttributeSanity(propertyModel)) { + String resource = propertyModel.getResource(); + resource = capc.subst(resource); + URL resourceURL = Loader.getResourceBySelfClassLoader(resource); + if (resourceURL == null) { + addError("Could not find resource [" + resource + "]."); + } else { + try (InputStream istream = resourceURL.openStream();) { + PropertyModelHandlerHelper.loadAndSetProperties(capc, istream, scope); + } catch (IOException e) { + addError("Could not read resource file [" + resource + "].", e); + } + } + } else if (checkValueNameAttributesSanity(propertyModel)) { + // earlier versions performed Java '\' escapes for '\\' '\t' etc. Howevver, there is no + // need to do this. See RegularEscapeUtil.__UNUSED__basicEscape + String value = propertyModel.getValue(); + + // now remove both leading and trailing spaces + value = value.trim(); + value = capc.subst(value); + ActionUtil.setProperty(capc, propertyModel.getName(), value, scope); + + } else { + addError(ModelConstants.INVALID_ATTRIBUTES); + } + } + + public static boolean checkFileAttributeSanity(PropertyModel propertyModel) { + String file = propertyModel.getFile(); + String name = propertyModel.getName(); + String value = propertyModel.getValue(); + String resource = propertyModel.getResource(); + + return !(OptionHelper.isNullOrEmptyOrAllSpaces(file)) && (OptionHelper.isNullOrEmptyOrAllSpaces(name) + && OptionHelper.isNullOrEmptyOrAllSpaces(value) && OptionHelper.isNullOrEmptyOrAllSpaces(resource)); + } + + public static boolean checkResourceAttributeSanity(PropertyModel propertyModel) { + String file = propertyModel.getFile(); + String name = propertyModel.getName(); + String value = propertyModel.getValue(); + String resource = propertyModel.getResource(); + + return !(OptionHelper.isNullOrEmptyOrAllSpaces(resource)) && (OptionHelper.isNullOrEmptyOrAllSpaces(name) + && OptionHelper.isNullOrEmptyOrAllSpaces(value) && OptionHelper.isNullOrEmptyOrAllSpaces(file)); + } + + public static boolean checkValueNameAttributesSanity(PropertyModel propertyModel) { + String file = propertyModel.getFile(); + String name = propertyModel.getName(); + String value = propertyModel.getValue(); + String resource = propertyModel.getResource(); + return (!(OptionHelper.isNullOrEmptyOrAllSpaces(name) || OptionHelper.isNullOrEmptyOrAllSpaces(value)) && ( + OptionHelper.isNullOrEmptyOrAllSpaces(file) && OptionHelper.isNullOrEmptyOrAllSpaces(resource))); + } + + /** + * Add all the properties found in the argument named 'props' to an InterpretationContext. + */ + static public void setProperty(ContextAwarePropertyContainer capc, String key, String value, + ActionUtil.Scope scope) { + switch (scope) { + case LOCAL: + capc.addSubstitutionProperty(key, value); + break; + case CONTEXT: + capc.getContext().putProperty(key, value); + break; + case SYSTEM: + OptionHelper.setSystemProperty(capc, key, value); + } + } + + /** + * Add all the properties found in the argument named 'props' to an InterpretationContext. + */ + static public void setProperties(ContextAwarePropertyContainer capc, Properties props, ActionUtil.Scope scope) { + switch (scope) { + case LOCAL: + capc.addSubstitutionProperties(props); + break; + case CONTEXT: + ContextUtil cu = new ContextUtil(capc.getContext()); + cu.addProperties(props); + break; + case SYSTEM: + OptionHelper.setSystemProperties(capc, props); + } + } + + static public void loadAndSetProperties(ContextAwarePropertyContainer capc, InputStream istream, + ActionUtil.Scope scope) throws IOException { + Properties props = new Properties(); + props.load(istream); + PropertyModelHandlerHelper.setProperties(capc, props, scope); + } +} diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java b/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java deleted file mode 100644 index 5c4328c882..0000000000 --- a/logback-core/src/main/java/ch/qos/logback/core/model/util/PropertyModelUtil.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.core.model.util; - -import ch.qos.logback.core.joran.action.ActionUtil; -import ch.qos.logback.core.model.PropertyModel; -import ch.qos.logback.core.spi.ContextAwarePropertyContainer; -import ch.qos.logback.core.util.ContextUtil; -import ch.qos.logback.core.util.OptionHelper; - -import java.util.Properties; - -/** - * - * - * @since 1.5.1 - */ -public class PropertyModelUtil { - - - public static boolean checkFileAttributeSanity(PropertyModel propertyModel) { - String file = propertyModel.getFile(); - String name = propertyModel.getName(); - String value = propertyModel.getValue(); - String resource = propertyModel.getResource(); - - return !(OptionHelper.isNullOrEmptyOrAllSpaces(file)) && (OptionHelper.isNullOrEmptyOrAllSpaces(name) - && OptionHelper.isNullOrEmptyOrAllSpaces(value) && OptionHelper.isNullOrEmptyOrAllSpaces(resource)); - } - - public static boolean checkResourceAttributeSanity(PropertyModel propertyModel) { - String file = propertyModel.getFile(); - String name = propertyModel.getName(); - String value = propertyModel.getValue(); - String resource = propertyModel.getResource(); - - return !(OptionHelper.isNullOrEmptyOrAllSpaces(resource)) && (OptionHelper.isNullOrEmptyOrAllSpaces(name) - && OptionHelper.isNullOrEmptyOrAllSpaces(value) && OptionHelper.isNullOrEmptyOrAllSpaces(file)); - } - - public static boolean checkValueNameAttributesSanity(PropertyModel propertyModel) { - String file = propertyModel.getFile(); - String name = propertyModel.getName(); - String value = propertyModel.getValue(); - String resource = propertyModel.getResource(); - return (!(OptionHelper.isNullOrEmptyOrAllSpaces(name) || OptionHelper.isNullOrEmptyOrAllSpaces(value)) - && (OptionHelper.isNullOrEmptyOrAllSpaces(file) && OptionHelper.isNullOrEmptyOrAllSpaces(resource))); - } - - /** - * Add all the properties found in the argument named 'props' to an - * InterpretationContext. - */ - static public void setProperty(ContextAwarePropertyContainer capc, String key, String value, ActionUtil.Scope scope) { - switch (scope) { - case LOCAL: - capc.addSubstitutionProperty(key, value); - break; - case CONTEXT: - capc.getContext().putProperty(key, value); - break; - case SYSTEM: - OptionHelper.setSystemProperty(capc, key, value); - } - } - - /** - * Add all the properties found in the argument named 'props' to an - * InterpretationContext. - */ - static public void setProperties(ContextAwarePropertyContainer capc, Properties props, ActionUtil.Scope scope) { - switch (scope) { - case LOCAL: - capc.addSubstitutionProperties(props); - break; - case CONTEXT: - ContextUtil cu = new ContextUtil(capc.getContext()); - cu.addProperties(props); - break; - case SYSTEM: - OptionHelper.setSystemProperties(capc, props); - } - } -} diff --git a/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java b/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java index cca9beb3e1..b0fd93a79c 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java +++ b/logback-core/src/main/java/ch/qos/logback/core/model/util/VariableSubstitutionsHelper.java @@ -17,13 +17,11 @@ import ch.qos.logback.core.Context; import ch.qos.logback.core.spi.ContextAwareBase; import ch.qos.logback.core.spi.ContextAwarePropertyContainer; -import ch.qos.logback.core.spi.PropertyContainer; import ch.qos.logback.core.spi.ScanException; import ch.qos.logback.core.util.OptionHelper; import java.util.HashMap; import java.util.Map; -import java.util.Properties; /** * Helper methods to deal with properties. @@ -44,6 +42,7 @@ public VariableSubstitutionsHelper(Context context, Map otherMap this.propertiesMap = new HashMap<>(otherMap); } + @Override public String subst(String ref) { if (ref == null) { return null; diff --git a/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java b/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java index 6590361561..1763d64ef7 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java +++ b/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwarePropertyContainer.java @@ -20,4 +20,14 @@ * @since 1.5.1 */ public interface ContextAwarePropertyContainer extends PropertyContainer, ContextAware { + + + /** + * This method is used tp perform variable substitution. + * + * @param input + * @return a new string after variable substitution, if any. + */ + String subst(String input); + } diff --git a/logback-core/src/main/java/ch/qos/logback/core/util/StringUtil.java b/logback-core/src/main/java/ch/qos/logback/core/util/StringUtil.java index 4b44cceaec..3721fc7a96 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/util/StringUtil.java +++ b/logback-core/src/main/java/ch/qos/logback/core/util/StringUtil.java @@ -14,11 +14,20 @@ package ch.qos.logback.core.util; +import ch.qos.logback.core.CoreConstants; + /** * @since 1.5.0 */ public class StringUtil { + public static String nullStringToEmpty(String input) { + if (input != null) + return input; + else + return CoreConstants.EMPTY_STRING; + } + /** * Returns true if input str is null or empty. * @@ -43,7 +52,7 @@ public static String capitalizeFirstLetter(String name) { if (isNullOrEmpty(name)) return name; - if(name.length() == 1) { + if (name.length() == 1) { return name.toUpperCase(); } else return name.substring(0, 1).toUpperCase() + name.substring(1); @@ -53,7 +62,7 @@ public static String lowercaseFirstLetter(String name) { if (isNullOrEmpty(name)) return name; - if(name.length() == 1) { + if (name.length() == 1) { return name.toLowerCase(); } else return name.substring(0, 1).toLowerCase() + name.substring(1);