-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
- Loading branch information
Showing
12 changed files
with
410 additions
and
82 deletions.
There are no files selected for viewing
106 changes: 105 additions & 1 deletion
106
logback-classic/src/main/java/ch/qos/logback/classic/tyler/TylerConfiguratorBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,106 @@ | ||
package ch.qos.logback.classic.tyler;public class TylerConfiguratorBase { | ||
/* | ||
* 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.tyler; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
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.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 static final String SET_CONTEXT_NAME = "setContextName"; | ||
public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger"; | ||
|
||
VariableSubstitutionsHelper variableSubstitutionsHelper; | ||
|
||
private Logger setupLogger(String loggerName, Level level, String levelString, Boolean additivity) { | ||
LoggerContext loggerContext = (LoggerContext) context; | ||
Logger logger = loggerContext.getLogger(loggerName); | ||
if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) { | ||
logger.setLevel(level); | ||
} | ||
if (additivity != null) { | ||
logger.setAdditive(additivity); | ||
} | ||
return logger; | ||
} | ||
|
||
protected void setContextName(String name) { | ||
if(StringUtil.isNullOrEmpty(name)) { | ||
addError("Cannot set context name to null or empty string"); | ||
return; | ||
} | ||
try { | ||
String substName = subst(name); | ||
addInfo("Setting context name to ["+substName+"]"); | ||
context.setName(substName); | ||
} catch (IllegalStateException e) { | ||
addError("Failed to rename context as [" + name + "]"); | ||
} | ||
} | ||
|
||
protected void addOnConsoleStatusListener() { | ||
StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener()); | ||
} | ||
|
||
/** | ||
* Performs variable substitution. | ||
* | ||
* @param ref | ||
* @return | ||
*/ | ||
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; | ||
} | ||
} | ||
|
||
@Override | ||
public void addSubstitutionProperty(String key, String value) { | ||
variableSubstitutionsHelper.addSubstitutionProperty(key, value); | ||
} | ||
|
||
/** | ||
* If a key is found in propertiesMap then return it. | ||
*/ | ||
@Override | ||
public String getProperty(String key) { | ||
return variableSubstitutionsHelper.getProperty(key); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getCopyOfPropertyMap() { | ||
return variableSubstitutionsHelper.getCopyOfPropertyMap(); | ||
} | ||
|
||
} |
122 changes: 122 additions & 0 deletions
122
logback-classic/src/main/java/ch/qos/logback/classic/tyler/VariableModelHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* 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.tyler; | ||
|
||
import ch.qos.logback.core.Context; | ||
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.VariableSubstitutionsHelper; | ||
import ch.qos.logback.core.spi.ContextAwareBase; | ||
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; | ||
|
||
public class VariableModelHelper extends ContextAwareBase { | ||
|
||
TylerConfiguratorBase tylerConfiguratorBase; | ||
VariableSubstitutionsHelper variableSubstitutionsHelper; | ||
|
||
VariableModelHelper(Context context, TylerConfiguratorBase tylerConfiguratorBase) { | ||
super( tylerConfiguratorBase); | ||
this.context = context; | ||
this.tylerConfiguratorBase = tylerConfiguratorBase; | ||
this.variableSubstitutionsHelper = new VariableSubstitutionsHelper(context); | ||
} | ||
|
||
void updateProperties(PropertyModel propertyModel) { | ||
|
||
ActionUtil.Scope scope = ActionUtil.stringToScope(propertyModel.getScopeStr()); | ||
if (PropertyModelUtil.checkFileAttributeSanity(propertyModel)) { | ||
String file = propertyModel.getFile(); | ||
file = tylerConfiguratorBase.subst(file); | ||
try (FileInputStream istream = new FileInputStream(file)) { | ||
loadAndSetProperties(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 = tylerConfiguratorBase.subst(resource); | ||
URL resourceURL = Loader.getResourceBySelfClassLoader(resource); | ||
if (resourceURL == null) { | ||
addError("Could not find resource [" + resource + "]."); | ||
} else { | ||
try ( InputStream istream = resourceURL.openStream();) { | ||
loadAndSetProperties(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 = tylerConfiguratorBase.subst(value); | ||
setProperty(propertyModel.getName(), value, scope); | ||
|
||
} else { | ||
addError(ModelConstants.INVALID_ATTRIBUTES); | ||
} | ||
} | ||
|
||
void loadAndSetProperties(InputStream istream, ActionUtil.Scope scope) throws IOException { | ||
Properties props = new Properties(); | ||
props.load(istream); | ||
setProperties(props, scope); | ||
} | ||
|
||
|
||
public void setProperties(Properties props, ActionUtil.Scope scope) { | ||
switch (scope) { | ||
case LOCAL: | ||
variableSubstitutionsHelper.addSubstitutionProperties(props); | ||
break; | ||
case CONTEXT: | ||
ContextUtil cu = new ContextUtil(getContext()); | ||
cu.addProperties(props); | ||
break; | ||
case SYSTEM: | ||
OptionHelper.setSystemProperties(this, props); | ||
} | ||
} | ||
|
||
public void setProperty(String key, String value, ActionUtil.Scope scope) { | ||
switch (scope) { | ||
case LOCAL: | ||
variableSubstitutionsHelper.addSubstitutionProperty(key, value); | ||
break; | ||
case CONTEXT: | ||
getContext().putProperty(key, value); | ||
break; | ||
case SYSTEM: | ||
OptionHelper.setSystemProperty(this, key, value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.