diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java index f0e40a7..3ef6b6d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java @@ -67,7 +67,7 @@ public final class PropertiesRewritePolicy implements RewritePolicy { for (final Map.Entry entry : properties.entrySet()) { final Property prop = entry.getKey(); props.put(prop.getName(), entry.getValue().booleanValue() ? - config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue()); + config.getStrSubstitutor().replace(config, prop.getValue()) : prop.getValue()); } final LogEvent result = new Log4jLogEvent.Builder(source).setContextMap(props).build(); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java index e014bd7..c2d7dac 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/DefaultRolloverStrategy.java @@ -293,8 +293,8 @@ public class DefaultRolloverStrategy implements RolloverStrategy { final StringBuilder buf = new StringBuilder(); // LOG4J2-531: directory scan & rollover must use same format - manager.getPatternProcessor().formatFileName(subst, buf, highIndex); - String highFilename = subst.replace(buf); + manager.getPatternProcessor().formatFileName(null, subst, buf, highIndex); + String highFilename = subst.replace(null, buf); final int suffixLength = suffixLength(highFilename); int maxIndex = 0; @@ -344,9 +344,9 @@ public class DefaultRolloverStrategy implements RolloverStrategy { // add a rename action to the list buf.setLength(0); // LOG4J2-531: directory scan & rollover must use same format - manager.getPatternProcessor().formatFileName(subst, buf, i - 1); + manager.getPatternProcessor().formatFileName(null, subst, buf, i - 1); - final String lowFilename = subst.replace(buf); + final String lowFilename = subst.replace(null, buf); String renameTo = lowFilename; if (isBase) { @@ -358,9 +358,9 @@ public class DefaultRolloverStrategy implements RolloverStrategy { } else { buf.setLength(0); // LOG4J2-531: directory scan & rollover must use same format - manager.getPatternProcessor().formatFileName(subst, buf, i - 1); + manager.getPatternProcessor().formatFileName(null, subst, buf, i - 1); - highFilename = subst.replace(buf); + highFilename = subst.replace(null, buf); } } if (maxIndex == 0) { @@ -400,9 +400,9 @@ public class DefaultRolloverStrategy implements RolloverStrategy { final StringBuilder buf = new StringBuilder(); // LOG4J2-531: directory scan & rollover must use same format - manager.getPatternProcessor().formatFileName(subst, buf, lowIndex); + manager.getPatternProcessor().formatFileName(null, subst, buf, lowIndex); - String lowFilename = subst.replace(buf); + String lowFilename = subst.replace(null, buf); final int suffixLength = suffixLength(lowFilename); for (int i = lowIndex; i <= highIndex; i++) { @@ -445,9 +445,9 @@ public class DefaultRolloverStrategy implements RolloverStrategy { // add a rename action to the list buf.setLength(0); // LOG4J2-531: directory scan & rollover must use same format - manager.getPatternProcessor().formatFileName(subst, buf, i + 1); + manager.getPatternProcessor().formatFileName(null, subst, buf, i + 1); - final String highFilename = subst.replace(buf); + final String highFilename = subst.replace(null, buf); String renameTo = highFilename; if (isBase) { @@ -512,7 +512,7 @@ public class DefaultRolloverStrategy implements RolloverStrategy { LOGGER.trace("DefaultRolloverStrategy.purge() took {} milliseconds", durationMillis); } final StringBuilder buf = new StringBuilder(255); - manager.getPatternProcessor().formatFileName(subst, buf, fileIndex); + manager.getPatternProcessor().formatFileName(null, subst, buf, fileIndex); final String currentFileName = manager.getFileName(); String renameTo = buf.toString(); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java index af5bcdb..dda94f0 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/PatternProcessor.java @@ -24,6 +24,7 @@ import java.util.List; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.lookup.StrSubstitutor; import org.apache.logging.log4j.core.pattern.ArrayPatternConverter; @@ -206,13 +207,13 @@ public class PatternProcessor { * @param buf string buffer to which formatted file name is appended, may not be null. * @param obj object to be evaluated in formatting, may not be null. */ - public final void formatFileName(final StrSubstitutor subst, final StringBuilder buf, final Object obj) { + public final void formatFileName(final Configuration config, final StrSubstitutor subst, final StringBuilder buf, final Object obj) { // LOG4J2-628: we deliberately use System time, not the log4j.Clock time // for creating the file name of rolled-over files. final long time = prevFileTime == 0 ? System.currentTimeMillis() : prevFileTime; formatFileName(buf, new Date(time), obj); final LogEvent event = new Log4jLogEvent.Builder().setTimeMillis(time).build(); - final String fileName = subst.replace(event, buf); + final String fileName = subst.replace(config, event, buf); buf.setLength(0); buf.append(fileName); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/AbstractPathAction.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/AbstractPathAction.java index e66f543..6606eb5 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/AbstractPathAction.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/AbstractPathAction.java @@ -103,7 +103,7 @@ public abstract class AbstractPathAction extends AbstractAction { * @return the base path (all lookups resolved) */ public Path getBasePath() { - return Paths.get(subst.replace(getBasePathString())); + return Paths.get(subst.replace(null, getBasePathString())); } /** diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java index a6b5df0..96872ac 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/routing/RoutingAppender.java @@ -111,7 +111,7 @@ public final class RoutingAppender extends AbstractAppender { if (rewritePolicy != null) { event = rewritePolicy.rewrite(event); } - final String key = config.getStrSubstitutor().replace(event, routes.getPattern()); + final String key = config.getStrSubstitutor().replace(config, event, routes.getPattern()); final AppenderControl control = getControl(key, event); if (control != null) { control.callAppender(event); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java index 226ba08..167bb00 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java @@ -345,7 +345,7 @@ public class AsyncLogger extends Logger implements EventTranslatorVararg properties = privateConfig.loggerConfig.getProperties(); - event.mergePropertiesIntoContextMap(properties, privateConfig.config.getStrSubstitutor()); + event.mergePropertiesIntoContextMap(privateConfig.config, properties, privateConfig.config.getStrSubstitutor()); final ReliabilityStrategy strategy = privateConfig.loggerConfig.getReliabilityStrategy(); strategy.log(this, event); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java index a78582f..c6e901c 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/RingBufferLogEvent.java @@ -24,6 +24,7 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.ThreadContext.ContextStack; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.impl.ThrowableProxy; @@ -294,8 +295,8 @@ public class RingBufferLogEvent implements LogEvent { * @param properties configured properties * @param strSubstitutor used to lookup values of variables in properties */ - public void mergePropertiesIntoContextMap(final Map properties, - final StrSubstitutor strSubstitutor) { + public void mergePropertiesIntoContextMap(final Configuration config, final Map properties, + final StrSubstitutor strSubstitutor) { if (properties == null) { return; // nothing to do } @@ -308,7 +309,7 @@ public class RingBufferLogEvent implements LogEvent { if (map.containsKey(prop.getName())) { continue; // contextMap overrides config properties } - final String value = entry.getValue().booleanValue() ? strSubstitutor.replace(prop.getValue()) : prop + final String value = entry.getValue().booleanValue() ? strSubstitutor.replace(config, prop.getValue()) : prop .getValue(); map.put(prop.getName(), value); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java index 8f3d764..b3604b3 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationFactory.java @@ -389,7 +389,7 @@ public abstract class ConfigurationFactory extends ConfigurationBuilderFactory { public Configuration getConfiguration(final String name, final URI configLocation) { if (configLocation == null) { - final String configLocationStr = this.substitutor.replace(PropertiesUtil.getProperties() + final String configLocationStr = this.substitutor.replace(null, PropertiesUtil.getProperties() .getStringProperty(CONFIGURATION_FILE_PROPERTY)); if (configLocationStr != null) { ConfigurationSource source = null; diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java index ee100b4..8285aba 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/LoggerConfig.java @@ -332,7 +332,7 @@ public class LoggerConfig extends AbstractFilterable { LogEvent event = builder.build(); for (final Map.Entry entry : properties.entrySet()) { final Property prop = entry.getKey(); - final String value = entry.getValue() ? config.getStrSubstitutor().replace(event, prop.getValue()) + final String value = entry.getValue() ? config.getStrSubstitutor().replace(config, event, prop.getValue()) : prop.getValue(); props.add(Property.createProperty(prop.getName(), value)); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java index fcde1d5..87a3198 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/json/JsonConfiguration.java @@ -71,7 +71,7 @@ public class JsonConfiguration extends AbstractConfiguration implements Reconfig .withStatus(getDefaultStatus()); for (final Map.Entry entry : rootNode.getAttributes().entrySet()) { final String key = entry.getKey(); - final String value = getStrSubstitutor().replace(entry.getValue()); + final String value = getStrSubstitutor().replace(this, entry.getValue()); // TODO: this duplicates a lot of the XmlConfiguration constructor if ("status".equalsIgnoreCase(key)) { statusConfig.withStatus(value); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginAttributeVisitor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginAttributeVisitor.java index f4da42b..576bddf 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginAttributeVisitor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginAttributeVisitor.java @@ -40,7 +40,7 @@ public class PluginAttributeVisitor extends AbstractPluginVisitor attributes = node.getAttributes(); final String rawValue = removeAttributeValue(attributes, name, this.aliases); - final String replacedValue = this.substitutor.replace(event, rawValue); + final String replacedValue = this.substitutor.replace(configuration, event, rawValue); final Object defaultValue = findDefaultValue(event); final Object value = convert(replacedValue, defaultValue); final Object debugValue = this.annotation.sensitive() ? NameUtil.md5(value + this.getClass().getName()) : value; @@ -76,6 +76,6 @@ public class PluginAttributeVisitor extends AbstractPluginVisitor attributes = node.getAttributes(); final String rawValue = removeAttributeValue(attributes, name, this.aliases); - final String replacedValue = this.substitutor.replace(event, rawValue); + final String replacedValue = this.substitutor.replace(configuration, event, rawValue); final Object value = convert(replacedValue, null); final Object debugValue = this.annotation.sensitive() ? NameUtil.md5(value + this.getClass().getName()) : value; StringBuilders.appendKeyDqValue(log, name, debugValue); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginValueVisitor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginValueVisitor.java index 5ec53da..bd2fca2 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginValueVisitor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/visitors/PluginValueVisitor.java @@ -37,7 +37,7 @@ public class PluginValueVisitor extends AbstractPluginVisitor { final String name = this.annotation.value(); final String rawValue = node.getValue() != null ? node.getValue() : removeAttributeValue(node.getAttributes(), "value"); - final String value = this.substitutor.replace(event, rawValue); + final String value = this.substitutor.replace(configuration, event, rawValue); StringBuilders.appendKeyDqValue(log, name, value); return value; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java index 4369b89..16c7a7a 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/xml/XmlConfiguration.java @@ -112,7 +112,7 @@ public class XmlConfiguration extends AbstractConfiguration implements Reconfigu .withStatus(getDefaultStatus()); for (final Map.Entry entry : attrs.entrySet()) { final String key = entry.getKey(); - final String value = getStrSubstitutor().replace(entry.getValue()); + final String value = getStrSubstitutor().replace(this, entry.getValue()); if ("status".equalsIgnoreCase(key)) { statusConfig.withStatus(value); } else if ("dest".equalsIgnoreCase(key)) { diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLookup.java index 1dba499..3caf6c6 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/AbstractLookup.java @@ -16,6 +16,8 @@ */ package org.apache.logging.log4j.core.lookup; +import org.apache.logging.log4j.core.config.Configuration; + /** * A default lookup for others to extend. * @@ -26,11 +28,11 @@ public abstract class AbstractLookup implements StrLookup { /** * Calls {@code lookup(null, key)} in the super class. * - * @see StrLookup#lookup(LogEvent, String) + * @see StrLookup#lookup(Configuration, org.apache.logging.log4j.core.LogEvent, String) */ @Override - public String lookup(final String key) { - return lookup(null, key); + public String lookup(final Configuration config, final String key) { + return lookup(config, null, key); } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java index 9a39338..3670aeb 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ContextMapLookup.java @@ -18,6 +18,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; /** @@ -28,22 +29,24 @@ public class ContextMapLookup implements StrLookup { /** * Looks up the value from the ThreadContext Map. + * @para config The Configuration for which the lookup is being attempted * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override - public String lookup(final String key) { + public String lookup(final Configuration config, final String key) { return ThreadContext.get(key); } /** * Looks up the value from the ThreadContext Map. + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent. * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { return event.getContextMap().get(key); } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java index 3e630b0..64f56c1 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java @@ -24,6 +24,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.status.StatusLogger; @@ -38,22 +39,24 @@ public class DateLookup implements StrLookup { /** * Looks up the value of the environment variable. + * @param config The Configuration for which the lookup is being attempted * @param key the format to use. If null, the default DateFormat will be used. * @return The value of the environment variable. */ @Override - public String lookup(final String key) { + public String lookup(final Configuration config, final String key) { return formatDate(System.currentTimeMillis(), key); } /** * Looks up the value of the environment variable. + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent (is ignored by this StrLookup). * @param key the format to use. If null, the default DateFormat will be used. * @return The value of the environment variable. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { return formatDate(event.getTimeMillis(), key); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java index 1d3b811..cf713b9 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EnvironmentLookup.java @@ -17,6 +17,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; /** @@ -27,12 +28,13 @@ public class EnvironmentLookup extends AbstractLookup { /** * Looks up the value of the environment variable. + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent (is ignored by this StrLookup). * @param key the key to be looked up, may be null * @return The value of the environment variable. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { return System.getenv(key); } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java index ec2fbc0..62a69bf 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java @@ -22,6 +22,7 @@ import java.util.Map; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.util.PluginManager; import org.apache.logging.log4j.core.config.plugins.util.PluginType; import org.apache.logging.log4j.core.util.Loader; @@ -140,7 +141,7 @@ public class Interpolator extends AbstractLookup { * resolved */ @Override - public String lookup(final LogEvent event, String var) { + public String lookup(final Configuration config, final LogEvent event, String var) { if (var == null) { return null; } @@ -152,7 +153,7 @@ public class Interpolator extends AbstractLookup { final StrLookup lookup = lookups.get(prefix); String value = null; if (lookup != null) { - value = event == null ? lookup.lookup(name) : lookup.lookup(event, name); + value = event == null ? lookup.lookup(config, name) : lookup.lookup(config, event, name); } if (value != null) { @@ -161,7 +162,7 @@ public class Interpolator extends AbstractLookup { var = var.substring(prefixPos + 1); } if (defaultLookup != null) { - return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var); + return event == null ? defaultLookup.lookup(config, var) : defaultLookup.lookup(config, event, var); } return null; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java index 08e3885..3890ddb 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JavaLookup.java @@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.lookup; import java.util.Locale; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.util.Strings; @@ -68,7 +69,7 @@ public class JavaLookup extends AbstractLookup { } private String getSystemProperty(final String name) { - return spLookup.lookup(name); + return spLookup.lookup((Configuration)null, name); } private String getSystemProperty(final String prefix, final String name) { @@ -91,6 +92,8 @@ public class JavaLookup extends AbstractLookup { /** * Looks up the value of the environment variable. * + * @param config + * The Configuration for which the lookup is being attempted * @param event * The current LogEvent (is ignored by this StrLookup). * @param key @@ -98,7 +101,7 @@ public class JavaLookup extends AbstractLookup { * @return The value of the environment variable. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { switch (key) { case "version": return "Java version " + getSystemProperty("java.version"); diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JndiLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JndiLookup.java index d7d50cb..1fb8156 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JndiLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/JndiLookup.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.net.JndiManager; import org.apache.logging.log4j.status.StatusLogger; @@ -40,12 +41,13 @@ public class JndiLookup extends AbstractLookup { /** * Looks up the value of the JNDI resource. + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent (is ignored by this StrLookup). * @param key the JNDI resource name to be looked up, may be null * @return The value of the JNDI resource. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { if (key == null) { return null; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java index 4061900..8577bac 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java @@ -22,10 +22,9 @@ import java.net.URISyntaxException; import java.net.URL; import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.impl.ContextAnchor; import org.apache.logging.log4j.status.StatusLogger; /** @@ -56,41 +55,43 @@ public class Log4jLookup extends AbstractLookup { } @Override - public String lookup(final LogEvent event, final String key) { - final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get(); - if (ctx == null) { - return null; - } - final ConfigurationSource configSrc = ctx.getConfiguration().getConfigurationSource(); - final File file = configSrc.getFile(); - if (file != null) { - switch (key) { - case KEY_CONFIG_LOCATION: - return file.getAbsolutePath(); + public String lookup(final Configuration config, final LogEvent event, final String key) { + if(config != null) { + final ConfigurationSource configSrc = config.getConfigurationSource(); + final File file = configSrc.getFile(); + if (file != null) { + switch (key) { + case KEY_CONFIG_LOCATION: + return file.getAbsolutePath(); - case KEY_CONFIG_PARENT_LOCATION: - return file.getParentFile().getAbsolutePath(); + case KEY_CONFIG_PARENT_LOCATION: + return file.getParentFile().getAbsolutePath(); - default: - return null; + default: + return null; + } } - } - final URL url = configSrc.getURL(); - try { - switch (key) { - case KEY_CONFIG_LOCATION: - return asPath(url.toURI()); + final URL url = configSrc.getURL(); + if (url != null) { + try { + switch (key) { + case KEY_CONFIG_LOCATION: + return asPath(url.toURI()); - case KEY_CONFIG_PARENT_LOCATION: - return asPath(getParent(url.toURI())); + case KEY_CONFIG_PARENT_LOCATION: + return asPath(getParent(url.toURI())); - default: - return null; + default: + return null; + } + } catch (final URISyntaxException use) { + LOGGER.error(use); + return null; + } } - } catch (final URISyntaxException use) { - LOGGER.error(use); - return null; } + + return null; } } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java index a50de0d..7be9da4 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java @@ -19,6 +19,7 @@ package org.apache.logging.log4j.core.lookup; import java.util.Map; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; /** @@ -79,12 +80,12 @@ public class MainMapLookup extends MapLookup { } @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { return MAIN_SINGLETON.getMap().get(key); } @Override - public String lookup(final String key) { + public String lookup(final Configuration config, final String key) { return MAIN_SINGLETON.getMap().get(key); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java index c00645e..675eddb 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.message.MapMessage; @@ -117,7 +118,7 @@ public class MapLookup implements StrLookup { } @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { final boolean isMapMessage = event != null && event.getMessage() instanceof MapMessage; if (map == null && !isMapMessage) { return null; @@ -140,12 +141,14 @@ public class MapLookup implements StrLookup { * If the map is null, then null is returned. The map result object is converted to a string using toString(). *

* + * @param config + * the Configuration for which the lookup is being attempted * @param key * the key to be looked up, may be null * @return the matching value, null if no match */ @Override - public String lookup(final String key) { + public String lookup(final Configuration config, final String key) { if (map == null) { return null; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MarkerLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MarkerLookup.java index 320db49..ccfa275 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MarkerLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MarkerLookup.java @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; /** @@ -33,13 +34,13 @@ public class MarkerLookup extends AbstractLookup { static final String MARKER = "marker"; @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { final Marker marker = event == null ? null : event.getMarker(); return marker == null ? null : marker.getName(); } @Override - public String lookup(final String key) { + public String lookup(final Configuration config, final String key) { return MarkerManager.exists(key) ? key : null; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java index 83187c2..867c9ae 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookup.java @@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.status.StatusLogger; @@ -40,6 +41,8 @@ public class ResourceBundleLookup extends AbstractLookup { * * For example: "com.domain.messages:MyKey". * + * @param config + * The Configuration for which the lookup is being attempted * @param event * The current LogEvent. * @param key @@ -47,7 +50,7 @@ public class ResourceBundleLookup extends AbstractLookup { * @return The value associated with the key. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { if (key == null) { return null; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java index e29d280..5378c76 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrLookup.java @@ -17,6 +17,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; /** * Lookup a String key to a String value. @@ -66,10 +67,11 @@ public interface StrLookup { * map.put("number", new Integer(2)); * assertEquals("2", StrLookup.mapLookup(map).lookup("number")); * + * @param config The Configuration for which the lookup is being attempted * @param key the key to be looked up, may be null * @return the matching value, null if no match */ - String lookup(String key); + String lookup(Configuration config, String key); /** * Looks up a String key to a String value possibly using the current LogEvent. @@ -94,9 +96,10 @@ public interface StrLookup { * map.put("number", new Integer(2)); * assertEquals("2", StrLookup.mapLookup(map).lookup("number")); * + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent. * @param key the key to be looked up, may be null * @return the matching value, null if no match */ - String lookup(LogEvent event, String key); + String lookup(Configuration config, LogEvent event, String key); } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java index 0b9ead6..1d0d4ba 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Properties; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.util.Strings; /** @@ -327,12 +328,13 @@ public class StrSubstitutor { * Replaces all the occurrences of variables in the given source object with * their matching values from the map. * + * @param config The Configuration for which the lookup is being attempted * @param source the source text containing the variables to substitute, null returns null * @param valueMap the map with the values, may be null * @return the result of the replace operation */ - public static String replace(final Object source, final Map valueMap) { - return new StrSubstitutor(valueMap).replace(source); + public static String replace(final Configuration config, final Object source, final Map valueMap) { + return new StrSubstitutor(valueMap).replace(config, source); } /** @@ -340,6 +342,7 @@ public class StrSubstitutor { * their matching values from the map. This method allows to specify a * custom variable prefix and suffix * + * @param config The Configuration for which the lookup is being attempted * @param source the source text containing the variables to substitute, null returns null * @param valueMap the map with the values, may be null * @param prefix the prefix of variables, not null @@ -347,20 +350,21 @@ public class StrSubstitutor { * @return the result of the replace operation * @throws IllegalArgumentException if the prefix or suffix is null */ - public static String replace(final Object source, final Map valueMap, final String prefix, + public static String replace(final Configuration config, final Object source, final Map valueMap, final String prefix, final String suffix) { - return new StrSubstitutor(valueMap, prefix, suffix).replace(source); + return new StrSubstitutor(valueMap, prefix, suffix).replace(config, source); } /** * Replaces all the occurrences of variables in the given source object with their matching * values from the properties. * + * @param config The Configuration for which the lookup is being attempted * @param source the source text containing the variables to substitute, null returns null * @param valueProperties the properties with values, may be null * @return the result of the replace operation */ - public static String replace(final Object source, final Properties valueProperties) { + public static String replace(final Configuration config, final Object source, final Properties valueProperties) { if (valueProperties == null) { return source.toString(); } @@ -371,7 +375,7 @@ public class StrSubstitutor { final String propValue = valueProperties.getProperty(propName); valueMap.put(propName, propValue); } - return StrSubstitutor.replace(source, valueMap); + return StrSubstitutor.replace(config, source, valueMap); } //----------------------------------------------------------------------- @@ -379,27 +383,29 @@ public class StrSubstitutor { * Replaces all the occurrences of variables with their matching values * from the resolver using the given source string as a template. * + * @param config The Configuration for which the lookup is being attempted * @param source the string to replace in, null returns null * @return the result of the replace operation */ - public String replace(final String source) { - return replace(null, source); + public String replace(final Configuration config, final String source) { + return replace(null, null, source); } //----------------------------------------------------------------------- /** * Replaces all the occurrences of variables with their matching values * from the resolver using the given source string as a template. * + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent if there is one. * @param source the string to replace in, null returns null * @return the result of the replace operation */ - public String replace(final LogEvent event, final String source) { + public String replace(final Configuration config, final LogEvent event, final String source) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(source); - if (!substitute(event, buf, 0, source.length())) { + if (!substitute(config, event, buf, 0, source.length())) { return source; } return buf.toString(); @@ -413,13 +419,14 @@ public class StrSubstitutor { * The rest of the string is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param source the string to replace in, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final String source, final int offset, final int length) { - return replace(null, source, offset, length); + public String replace(final Configuration config, final String source, final int offset, final int length) { + return replace(config, null, source, offset, length); } /** @@ -430,18 +437,19 @@ public class StrSubstitutor { * The rest of the string is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the string to replace in, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final LogEvent event, final String source, final int offset, final int length) { + public String replace(final Configuration config, final LogEvent event, final String source, final int offset, final int length) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(length).append(source, offset, length); - if (!substitute(event, buf, 0, length)) { + if (!substitute(config, event, buf, 0, length)) { return source.substring(offset, offset + length); } return buf.toString(); @@ -453,11 +461,12 @@ public class StrSubstitutor { * from the resolver using the given source array as a template. * The array is not altered by this method. * + * @param config The Configuration for which the lookup is being attempted * @param source the character array to replace in, not altered, null returns null * @return the result of the replace operation */ - public String replace(final char[] source) { - return replace(null, source); + public String replace(final Configuration config, final char[] source) { + return replace(config, null, source); } //----------------------------------------------------------------------- @@ -466,16 +475,17 @@ public class StrSubstitutor { * from the resolver using the given source array as a template. * The array is not altered by this method. * + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the character array to replace in, not altered, null returns null * @return the result of the replace operation */ - public String replace(final LogEvent event, final char[] source) { + public String replace(final Configuration config, final LogEvent event, final char[] source) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(source.length).append(source); - substitute(event, buf, 0, source.length); + substitute(config, event, buf, 0, source.length); return buf.toString(); } @@ -488,13 +498,14 @@ public class StrSubstitutor { * The rest of the array is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param source the character array to replace in, not altered, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final char[] source, final int offset, final int length) { - return replace(null, source, offset, length); + public String replace(final Configuration config, final char[] source, final int offset, final int length) { + return replace(config, null, source, offset, length); } /** @@ -506,18 +517,19 @@ public class StrSubstitutor { * The rest of the array is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the character array to replace in, not altered, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final LogEvent event, final char[] source, final int offset, final int length) { + public String replace(final Configuration config, final LogEvent event, final char[] source, final int offset, final int length) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(length).append(source, offset, length); - substitute(event, buf, 0, length); + substitute(config, event, buf, 0, length); return buf.toString(); } @@ -527,11 +539,12 @@ public class StrSubstitutor { * from the resolver using the given source buffer as a template. * The buffer is not altered by this method. * + * @param config The Configuration for which the lookup is being attempted * @param source the buffer to use as a template, not changed, null returns null * @return the result of the replace operation */ - public String replace(final StringBuffer source) { - return replace(null, source); + public String replace(final Configuration config, final StringBuffer source) { + return replace(config, null, source); } //----------------------------------------------------------------------- @@ -540,16 +553,17 @@ public class StrSubstitutor { * from the resolver using the given source buffer as a template. * The buffer is not altered by this method. * + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the buffer to use as a template, not changed, null returns null * @return the result of the replace operation */ - public String replace(final LogEvent event, final StringBuffer source) { + public String replace(final Configuration config, final LogEvent event, final StringBuffer source) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(source.length()).append(source); - substitute(event, buf, 0, buf.length()); + substitute(config, event, buf, 0, buf.length()); return buf.toString(); } @@ -562,13 +576,14 @@ public class StrSubstitutor { * The rest of the buffer is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param source the buffer to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final StringBuffer source, final int offset, final int length) { - return replace(null, source, offset, length); + public String replace(final Configuration config, final StringBuffer source, final int offset, final int length) { + return replace(config, null, source, offset, length); } /** @@ -580,18 +595,19 @@ public class StrSubstitutor { * The rest of the buffer is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the buffer to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final LogEvent event, final StringBuffer source, final int offset, final int length) { + public String replace(final Configuration config, final LogEvent event, final StringBuffer source, final int offset, final int length) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(length).append(source, offset, length); - substitute(event, buf, 0, length); + substitute(config, event, buf, 0, length); return buf.toString(); } @@ -601,11 +617,12 @@ public class StrSubstitutor { * from the resolver using the given source builder as a template. * The builder is not altered by this method. * + * @param config The Configuration for which the lookup is being attempted * @param source the builder to use as a template, not changed, null returns null * @return the result of the replace operation */ - public String replace(final StringBuilder source) { - return replace(null, source); + public String replace(final Configuration config, final StringBuilder source) { + return replace(config, null, source); } //----------------------------------------------------------------------- @@ -614,16 +631,17 @@ public class StrSubstitutor { * from the resolver using the given source builder as a template. * The builder is not altered by this method. * + * @param config The Configuration for which the lookup is being attempted * @param event The LogEvent. * @param source the builder to use as a template, not changed, null returns null. * @return the result of the replace operation. */ - public String replace(final LogEvent event, final StringBuilder source) { + public String replace(final Configuration config, final LogEvent event, final StringBuilder source) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(source.length()).append(source); - substitute(event, buf, 0, buf.length()); + substitute(config, event, buf, 0, buf.length()); return buf.toString(); } /** @@ -635,13 +653,14 @@ public class StrSubstitutor { * The rest of the builder is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param source the builder to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final StringBuilder source, final int offset, final int length) { - return replace(null, source, offset, length); + public String replace(final Configuration config, final StringBuilder source, final int offset, final int length) { + return replace(config, null, source, offset, length); } /** @@ -653,18 +672,19 @@ public class StrSubstitutor { * The rest of the builder is not processed, and is not returned. *

* + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the builder to use as a template, not changed, null returns null * @param offset the start offset within the array, must be valid * @param length the length within the array to be processed, must be valid * @return the result of the replace operation */ - public String replace(final LogEvent event, final StringBuilder source, final int offset, final int length) { + public String replace(final Configuration config, final LogEvent event, final StringBuilder source, final int offset, final int length) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder(length).append(source, offset, length); - substitute(event, buf, 0, length); + substitute(config, event, buf, 0, length); return buf.toString(); } @@ -674,11 +694,12 @@ public class StrSubstitutor { * their matching values from the resolver. The input source object is * converted to a string using toString and is not altered. * + * @param config The Configuration for which the lookup is being attempted * @param source the source to replace in, null returns null * @return the result of the replace operation */ - public String replace(final Object source) { - return replace(null, source); + public String replace(final Configuration config, final Object source) { + return replace(config, null, source); } //----------------------------------------------------------------------- /** @@ -686,16 +707,17 @@ public class StrSubstitutor { * their matching values from the resolver. The input source object is * converted to a string using toString and is not altered. * + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the source to replace in, null returns null * @return the result of the replace operation */ - public String replace(final LogEvent event, final Object source) { + public String replace(final Configuration config, final LogEvent event, final Object source) { if (source == null) { return null; } final StringBuilder buf = new StringBuilder().append(source); - substitute(event, buf, 0, buf.length()); + substitute(config, event, buf, 0, buf.length()); return buf.toString(); } @@ -708,11 +730,11 @@ public class StrSubstitutor { * @param source the buffer to replace in, updated, null returns zero * @return true if altered */ - public boolean replaceIn(final StringBuffer source) { + public boolean replaceIn(final Configuration config, final StringBuffer source) { if (source == null) { return false; } - return replaceIn(source, 0, source.length()); + return replaceIn(config, source, 0, source.length()); } /** @@ -724,13 +746,14 @@ public class StrSubstitutor { * The rest of the buffer is not processed, but it is not deleted. *

* + * @param config The Configuration for which the lookup is being attempted * @param source the buffer to replace in, updated, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the buffer to be processed, must be valid * @return true if altered */ - public boolean replaceIn(final StringBuffer source, final int offset, final int length) { - return replaceIn(null, source, offset, length); + public boolean replaceIn(final Configuration config, final StringBuffer source, final int offset, final int length) { + return replaceIn(config, null, source, offset, length); } /** @@ -742,18 +765,19 @@ public class StrSubstitutor { * The rest of the buffer is not processed, but it is not deleted. *

* + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the buffer to replace in, updated, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the buffer to be processed, must be valid * @return true if altered */ - public boolean replaceIn(final LogEvent event, final StringBuffer source, final int offset, final int length) { + public boolean replaceIn(final Configuration config, final LogEvent event, final StringBuffer source, final int offset, final int length) { if (source == null) { return false; } final StringBuilder buf = new StringBuilder(length).append(source, offset, length); - if (!substitute(event, buf, 0, length)) { + if (!substitute(config, event, buf, 0, length)) { return false; } source.replace(offset, offset + length, buf.toString()); @@ -765,11 +789,12 @@ public class StrSubstitutor { * Replaces all the occurrences of variables within the given source * builder with their matching values from the resolver. * + * @param config The Configuration for which the lookup is being attempted * @param source the builder to replace in, updated, null returns zero * @return true if altered */ - public boolean replaceIn(final StringBuilder source) { - return replaceIn(null, source); + public boolean replaceIn(final Configuration config, final StringBuilder source) { + return replaceIn(config, null, source); } //----------------------------------------------------------------------- @@ -777,15 +802,16 @@ public class StrSubstitutor { * Replaces all the occurrences of variables within the given source * builder with their matching values from the resolver. * + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one exists. * @param source the builder to replace in, updated, null returns zero * @return true if altered */ - public boolean replaceIn(final LogEvent event, final StringBuilder source) { + public boolean replaceIn(final Configuration config, final LogEvent event, final StringBuilder source) { if (source == null) { return false; } - return substitute(event, source, 0, source.length()); + return substitute(config, event, source, 0, source.length()); } /** * Replaces all the occurrences of variables within the given source @@ -795,12 +821,13 @@ public class StrSubstitutor { * The rest of the builder is not processed, but it is not deleted. *

* + * @param config The Configuration for which the lookup is being attempted * @param source the builder to replace in, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ - public boolean replaceIn(final StringBuilder source, final int offset, final int length) { + public boolean replaceIn(final Configuration config, final StringBuilder source, final int offset, final int length) { return replaceIn(null, source, offset, length); } @@ -812,17 +839,18 @@ public class StrSubstitutor { * The rest of the builder is not processed, but it is not deleted. *

* + * @param config The Configuration for which the lookup is being attempted * @param event the current LogEvent, if one is present. * @param source the builder to replace in, null returns zero * @param offset the start offset within the array, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ - public boolean replaceIn(final LogEvent event, final StringBuilder source, final int offset, final int length) { + public boolean replaceIn(final Configuration config, final LogEvent event, final StringBuilder source, final int offset, final int length) { if (source == null) { return false; } - return substitute(event, source, offset, length); + return substitute(config, event, source, offset, length); } //----------------------------------------------------------------------- @@ -837,14 +865,15 @@ public class StrSubstitutor { * the substitution process at the start or end. *

* + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent, if there is one. * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ - protected boolean substitute(final LogEvent event, final StringBuilder buf, final int offset, final int length) { - return substitute(event, buf, offset, length, null) > 0; + protected boolean substitute(final Configuration config, final LogEvent event, final StringBuilder buf, final int offset, final int length) { + return substitute(config, event, buf, offset, length, null) > 0; } /** @@ -852,6 +881,7 @@ public class StrSubstitutor { * interpolation method, which resolves the values of all variable references * contained in the passed in text. * + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent, if there is one. * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid @@ -860,7 +890,7 @@ public class StrSubstitutor { * @return the length change that occurs, unless priorVariables is null when the int * represents a boolean flag as to whether any change occurred. */ - private int substitute(final LogEvent event, final StringBuilder buf, final int offset, final int length, + private int substitute(final Configuration config, final LogEvent event, final StringBuilder buf, final int offset, final int length, List priorVariables) { final StrMatcher prefixMatcher = getVariablePrefixMatcher(); final StrMatcher suffixMatcher = getVariableSuffixMatcher(); @@ -916,7 +946,7 @@ public class StrSubstitutor { - startMatchLen); if (substitutionInVariablesEnabled) { final StringBuilder bufName = new StringBuilder(varNameExpr); - substitute(event, bufName, 0, bufName.length()); + substitute(config, event, bufName, 0, bufName.length()); varNameExpr = bufName.toString(); } pos += endMatchLen; @@ -954,7 +984,7 @@ public class StrSubstitutor { priorVariables.add(varName); // resolve the variable - String varValue = resolveVariable(event, varName, buf, + String varValue = resolveVariable(config, event, varName, buf, startPos, endPos); if (varValue == null) { varValue = varDefaultValue; @@ -964,7 +994,7 @@ public class StrSubstitutor { final int varLen = varValue.length(); buf.replace(startPos, endPos, varValue); altered = true; - int change = substitute(event, buf, startPos, + int change = substitute(config, event, buf, startPos, varLen, priorVariables); change = change + (varLen - (endPos - startPos)); @@ -1031,13 +1061,13 @@ public class StrSubstitutor { * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or null if the variable is unknown */ - protected String resolveVariable(final LogEvent event, final String variableName, final StringBuilder buf, + protected String resolveVariable(final Configuration config, final LogEvent event, final String variableName, final StringBuilder buf, final int startPos, final int endPos) { final StrLookup resolver = getVariableResolver(); if (resolver == null) { return null; } - return resolver.lookup(event, variableName); + return resolver.lookup(config, event, variableName); } // Escape diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java index 379d6a9..6fe7370 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StructuredDataLookup.java @@ -17,6 +17,7 @@ package org.apache.logging.log4j.core.lookup; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.message.StructuredDataMessage; @@ -28,22 +29,24 @@ public class StructuredDataLookup implements StrLookup { /** * Returns {@code null}. This Lookup plugin does not make sense outside the context of a LogEvent. + * @param config the Configuration for which the lookup is being attempted * @param key the key to be looked up, may be null * @return {@code null} */ @Override - public String lookup(final String key) { + public String lookup(final Configuration config, final String key) { return null; } /** * Looks up the value for the key using the data in the LogEvent. + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent. * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { if (event == null || !(event.getMessage() instanceof StructuredDataMessage)) { return null; } diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java index d2fb530..7c74b68 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookup.java @@ -20,6 +20,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.status.StatusLogger; @@ -34,12 +35,13 @@ public class SystemPropertiesLookup extends AbstractLookup { /** * Looks up the value for the key using the data in the LogEvent. + * @param config The Configuration for which the lookup is being attempted * @param event The current LogEvent. * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { try { return System.getProperty(key); } catch (final Exception ex) { diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LiteralPatternConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LiteralPatternConverter.java index 0751bca..a38b76c 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LiteralPatternConverter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/LiteralPatternConverter.java @@ -55,7 +55,7 @@ public final class LiteralPatternConverter extends LogEventPatternConverter impl */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { - toAppendTo.append(substitute ? config.getStrSubstitutor().replace(event, literal) : literal); + toAppendTo.append(substitute ? config.getStrSubstitutor().replace(config, event, literal) : literal); } /** @@ -63,7 +63,7 @@ public final class LiteralPatternConverter extends LogEventPatternConverter impl */ @Override public void format(final Object obj, final StringBuilder output) { - output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal); + output.append(substitute ? config.getStrSubstitutor().replace(config, literal) : literal); } /** @@ -71,7 +71,7 @@ public final class LiteralPatternConverter extends LogEventPatternConverter impl */ @Override public void format(final StringBuilder output, final Object... objects) { - output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal); + output.append(substitute ? config.getStrSubstitutor().replace(config, literal) : literal); } public String getLiteral() { diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java index 4565e21..1745ccb 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/MessagePatternConverter.java @@ -70,7 +70,7 @@ public final class MessagePatternConverter extends LogEventPatternConverter { if (toAppendTo.charAt(i) == '$' && toAppendTo.charAt(i + 1) == '{') { final String value = toAppendTo.substring(offset, toAppendTo.length()); toAppendTo.setLength(offset); - toAppendTo.append(config.getStrSubstitutor().replace(event, value)); + toAppendTo.append(config.getStrSubstitutor().replace(config, event, value)); } } } @@ -85,7 +85,7 @@ public final class MessagePatternConverter extends LogEventPatternConverter { } if (result != null) { toAppendTo.append(config != null && result.contains("${") ? - config.getStrSubstitutor().replace(event, result) : result); + config.getStrSubstitutor().replace(config, event, result) : result); } else { toAppendTo.append("null"); } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java index b80491e..682d951 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ContextMapLookupTest.java @@ -70,9 +70,9 @@ public class ContextMapLookupTest { public void testLookup() { ThreadContext.put(TESTKEY, TESTVAL); final StrLookup lookup = new ContextMapLookup(); - String value = lookup.lookup(TESTKEY); + String value = lookup.lookup(null, TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("BadKey"); + value = lookup.lookup(null, "BadKey"); assertNull(value); } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java index 09794e2..2626f56 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/DateLookupTest.java @@ -34,7 +34,7 @@ public class DateLookupTest { public void testLookup() { final StrLookup lookup = new DateLookup(); final LogEvent event = new MyLogEvent(); - final String value = lookup.lookup(event, "MM/dd/yyyy"); + final String value = lookup.lookup(null, event, "MM/dd/yyyy"); assertNotNull(value); assertEquals("12/30/2011", value); } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java index 5cae1b1..adbcd7b 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/EnvironmentLookupTest.java @@ -29,9 +29,9 @@ public class EnvironmentLookupTest { @Test public void testLookup() { final StrLookup lookup = new EnvironmentLookup(); - String value = lookup.lookup("PATH"); + String value = lookup.lookup(null, "PATH"); assertNotNull(value); - value = lookup.lookup("BadKey"); + value = lookup.lookup(null, "BadKey"); assertNull(value); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java index 1fcadef..7c036b5 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java @@ -72,23 +72,23 @@ public class InterpolatorTest { map.put(TESTKEY, TESTVAL); final StrLookup lookup = new Interpolator(new MapLookup(map)); ThreadContext.put(TESTKEY, TESTVAL); - String value = lookup.lookup(TESTKEY); + String value = lookup.lookup(null, TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("ctx:" + TESTKEY); + value = lookup.lookup(null, "ctx:" + TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("sys:" + TESTKEY); + value = lookup.lookup(null, "sys:" + TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("BadKey"); + value = lookup.lookup(null, "BadKey"); assertNull(value); ThreadContext.clearMap(); - value = lookup.lookup("ctx:" + TESTKEY); + value = lookup.lookup(null, "ctx:" + TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("jndi:" + TEST_CONTEXT_RESOURCE_NAME); + value = lookup.lookup(null, "jndi:" + TEST_CONTEXT_RESOURCE_NAME); assertEquals(TEST_CONTEXT_NAME, value); } private void assertLookupNotEmpty(final StrLookup lookup, final String key) { - final String value = lookup.lookup(key); + final String value = lookup.lookup(null, key); assertNotNull(value); assertFalse(value.isEmpty()); System.out.println(key + " = " + value); @@ -97,13 +97,13 @@ public class InterpolatorTest { @Test public void testLookupWithDefaultInterpolator() { final StrLookup lookup = new Interpolator(); - String value = lookup.lookup("sys:" + TESTKEY); + String value = lookup.lookup(null, "sys:" + TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("env:PATH"); + value = lookup.lookup(null, "env:PATH"); assertNotNull(value); - value = lookup.lookup("jndi:" + TEST_CONTEXT_RESOURCE_NAME); + value = lookup.lookup(null, "jndi:" + TEST_CONTEXT_RESOURCE_NAME); assertEquals(TEST_CONTEXT_NAME, value); - value = lookup.lookup("date:yyyy-MM-dd"); + value = lookup.lookup(null, "date:yyyy-MM-dd"); assertNotNull("No Date", value); final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); final String today = format.format(new Date()); diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/JndiLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/JndiLookupTest.java index 97f6a3b..4705bee 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/JndiLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/JndiLookupTest.java @@ -57,13 +57,13 @@ public class JndiLookupTest { public void testLookup() { final StrLookup lookup = new JndiLookup(); - String contextName = lookup.lookup(TEST_CONTEXT_RESOURCE_NAME); + String contextName = lookup.lookup(null, TEST_CONTEXT_RESOURCE_NAME); assertEquals(TEST_CONTEXT_NAME, contextName); - contextName = lookup.lookup(JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME); + contextName = lookup.lookup(null, JndiLookup.CONTAINER_JNDI_RESOURCE_PATH_PREFIX + TEST_CONTEXT_RESOURCE_NAME); assertEquals(TEST_CONTEXT_NAME, contextName); - final String nonExistingResource = lookup.lookup("logging/non-existing-resource"); + final String nonExistingResource = lookup.lookup(null, "logging/non-existing-resource"); assertNull(nonExistingResource); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java index fc1ca36..014676b 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java @@ -43,27 +43,26 @@ public class Log4jLookupTest { private final static File EXPECT = new File(System.getProperty("user.home"), "/a/b/c/d/e/log4j2.xml"); private LoggerContext mockCtx = null; + private Configuration config = null; + private ConfigurationSource configSrc = null; @Before public void setup() throws URISyntaxException { this.mockCtx = EasyMock.createMock(LoggerContext.class); ContextAnchor.THREAD_CONTEXT.set(mockCtx); - final Configuration config = EasyMock.createMock(Configuration.class); - expect(mockCtx.getConfiguration()).andReturn(config); + this.config = EasyMock.createMock(Configuration.class); - final ConfigurationSource configSrc = EasyMock.createMock(ConfigurationSource.class); + this.configSrc = EasyMock.createMock(ConfigurationSource.class); expect(config.getConfigurationSource()).andReturn(configSrc); expect(configSrc.getFile()).andReturn(EXPECT); - replay(mockCtx); - replay(config); - replay(configSrc); + replay(mockCtx, config, configSrc); } @After public void cleanup() { - verify(mockCtx); + verify(mockCtx, config, configSrc); ContextAnchor.THREAD_CONTEXT.set(null); this.mockCtx = null; @@ -72,14 +71,14 @@ public class Log4jLookupTest { @Test public void lookupConfigLocation() { final StrLookup log4jLookup = new Log4jLookup(); - final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); + final String value = log4jLookup.lookup(config, KEY_CONFIG_LOCATION); assertEquals(EXPECT.getAbsolutePath(), value); } @Test public void lookupConfigParentLocation() { final StrLookup log4jLookup = new Log4jLookup(); - final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); + final String value = log4jLookup.lookup(config, KEY_CONFIG_PARENT_LOCATION); assertEquals(EXPECT.getParentFile().getAbsolutePath(), value); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java index 3e20159..217cead 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java @@ -41,27 +41,26 @@ public class Log4jLookupWithSpacesTest { private final static File EXPECT = new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e/log4j2 file.xml"); private LoggerContext mockCtx = null; + private Configuration config = null; + private ConfigurationSource configSrc = null; @Before public void setup() throws URISyntaxException, MalformedURLException { this.mockCtx = EasyMock.createMock(LoggerContext.class); ContextAnchor.THREAD_CONTEXT.set(mockCtx); - final Configuration config = EasyMock.createMock(Configuration.class); - expect(mockCtx.getConfiguration()).andReturn(config); + this.config = EasyMock.createMock(Configuration.class); - final ConfigurationSource configSrc = EasyMock.createMock(ConfigurationSource.class); + this.configSrc = EasyMock.createMock(ConfigurationSource.class); expect(config.getConfigurationSource()).andReturn(configSrc); expect(configSrc.getFile()).andReturn(EXPECT); - replay(mockCtx); - replay(config); - replay(configSrc); + replay(mockCtx, config, configSrc); } @After public void cleanup() { - verify(mockCtx); + verify(mockCtx, config, configSrc); ContextAnchor.THREAD_CONTEXT.set(null); this.mockCtx = null; @@ -70,7 +69,7 @@ public class Log4jLookupWithSpacesTest { @Test public void lookupConfigLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); - final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); + final String value = log4jLookup.lookup(config, KEY_CONFIG_LOCATION); assertEquals( new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e/log4j2 file.xml").getAbsolutePath(), value); @@ -79,7 +78,7 @@ public class Log4jLookupWithSpacesTest { @Test public void lookupConfigParentLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); - final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); + final String value = log4jLookup.lookup(config, KEY_CONFIG_PARENT_LOCATION); assertEquals(new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e").getAbsolutePath(), value); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsJmxLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsJmxLookupTest.java index be1f587..13b9563 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsJmxLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsJmxLookupTest.java @@ -36,15 +36,15 @@ public class MainInputArgumentsJmxLookupTest { @Test public void testMap() { final JmxRuntimeInputArgumentsLookup lookup = JmxRuntimeInputArgumentsLookup.JMX_SINGLETON; - assertEquals(null, lookup.lookup(null)); - assertEquals(null, lookup.lookup("X")); - assertEquals(null, lookup.lookup("foo.txt")); + assertEquals(null, lookup.lookup(null, null)); + assertEquals(null, lookup.lookup(null, "X")); + assertEquals(null, lookup.lookup(null, "foo.txt")); } public void callFromMain() { final JmxRuntimeInputArgumentsLookup lookup = JmxRuntimeInputArgumentsLookup.JMX_SINGLETON; - assertEquals(null, lookup.lookup(null)); - assertEquals(null, lookup.lookup("X")); + assertEquals(null, lookup.lookup(null, null)); + assertEquals(null, lookup.lookup(null, "X")); // Eclipse adds -Dfile.encoding=Cp1252 // assertEquals("--file", lookup.lookup("0")); // assertEquals("foo.txt", lookup.lookup("1")); diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MapLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MapLookupTest.java index 313b527..81fe478 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MapLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MapLookupTest.java @@ -33,8 +33,8 @@ public class MapLookupTest { @Test public void testEmptyMap() { final MapLookup lookup = new MapLookup(new HashMap()); - assertEquals(null, lookup.lookup(null)); - assertEquals(null, lookup.lookup("X")); + assertEquals(null, lookup.lookup(null, null)); + assertEquals(null, lookup.lookup(null, "X")); } @Test @@ -42,15 +42,15 @@ public class MapLookupTest { final HashMap map = new HashMap<>(); map.put("A", "B"); final MapLookup lookup = new MapLookup(map); - assertEquals(null, lookup.lookup(null)); - assertEquals("B", lookup.lookup("A")); + assertEquals(null, lookup.lookup(null, null)); + assertEquals("B", lookup.lookup(null, "A")); } @Test public void testNullMap() { final MapLookup lookup = new MapLookup(); - assertEquals(null, lookup.lookup(null)); - assertEquals(null, lookup.lookup("X")); + assertEquals(null, lookup.lookup(null, null)); + assertEquals(null, lookup.lookup(null, "X")); } @Test @@ -59,12 +59,12 @@ public class MapLookupTest { "--file", "foo.txt" }); final MapLookup lookup = MainMapLookup.MAIN_SINGLETON; - assertEquals(null, lookup.lookup(null)); - assertEquals(null, lookup.lookup("X")); - assertEquals("--file", lookup.lookup("0")); - assertEquals("foo.txt", lookup.lookup("1")); - assertEquals("foo.txt", lookup.lookup("--file")); - assertEquals(null, lookup.lookup("foo.txt")); + assertEquals(null, lookup.lookup(null, null)); + assertEquals(null, lookup.lookup(null, "X")); + assertEquals("--file", lookup.lookup(null, "0")); + assertEquals("foo.txt", lookup.lookup(null, "1")); + assertEquals("foo.txt", lookup.lookup(null, "--file")); + assertEquals(null, lookup.lookup(null, "foo.txt")); } @Test @@ -78,8 +78,8 @@ public class MapLookupTest { .setMessage(message) .build(); final MapLookup lookup = new MapLookup(map); - assertEquals("B", lookup.lookup(event, "A")); - assertEquals("B1", lookup.lookup(event, "A1")); + assertEquals("B", lookup.lookup(null, event, "A")); + assertEquals("B1", lookup.lookup(null, event, "A1")); } @Test diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MarkerLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MarkerLookupTest.java index d26f895..e2642bf 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MarkerLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MarkerLookupTest.java @@ -47,7 +47,7 @@ public class MarkerLookupTest { .setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")).build(); - final String value = strLookup.lookup(event, marker.getName()); + final String value = strLookup.lookup(null, event, marker.getName()); assertEquals(markerName, value); } @@ -58,7 +58,7 @@ public class MarkerLookupTest { .setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")).build(); - final String value = strLookup.lookup(event, ABSENT_MARKER_NAME); + final String value = strLookup.lookup(null, event, ABSENT_MARKER_NAME); assertNull(value); } @@ -71,7 +71,7 @@ public class MarkerLookupTest { .setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")).build(); - final String value = strLookup.lookup(event, ABSENT_MARKER_NAME); + final String value = strLookup.lookup(null, event, ABSENT_MARKER_NAME); assertEquals(markerName, value); } @@ -83,13 +83,13 @@ public class MarkerLookupTest { @Test public void testLookupExistant() { - final String value = strLookup.lookup(MarkerManager.getMarker(markerName).getName()); + final String value = strLookup.lookup(null, MarkerManager.getMarker(markerName).getName()); assertEquals(markerName, value); } @Test public void testLookupNonExistant() { - final String value = strLookup.lookup(ABSENT_MARKER_NAME); + final String value = strLookup.lookup(null, ABSENT_MARKER_NAME); assertNull(value); } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java index 31dcb13..2d0458d 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/ResourceBundleLookupTest.java @@ -27,25 +27,25 @@ public class ResourceBundleLookupTest { @Test public void testLookup() { final StrLookup lookup = new ResourceBundleLookup(); - final String value = lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle_en:KeyA"); - Assert.assertEquals("ValueA", lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyA")); + final String value = lookup.lookup(null, "org.apache.logging.log4j.core.lookup.resource-bundle_en:KeyA"); + Assert.assertEquals("ValueA", lookup.lookup(null, "org.apache.logging.log4j.core.lookup.resource-bundle:KeyA")); } @Test public void testLookupWithLocale() { final StrLookup lookup = new ResourceBundleLookup(); - final String value = lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyA"); - Assert.assertEquals("ValueA", lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyA")); + final String value = lookup.lookup(null, "org.apache.logging.log4j.core.lookup.resource-bundle:KeyA"); + Assert.assertEquals("ValueA", lookup.lookup(null, "org.apache.logging.log4j.core.lookup.resource-bundle:KeyA")); } public void testMissingKey() { final StrLookup lookup = new ResourceBundleLookup(); - Assert.assertNull(lookup.lookup("org.apache.logging.log4j.core.lookup.resource-bundle:KeyUnkown")); + Assert.assertNull(lookup.lookup(null, "org.apache.logging.log4j.core.lookup.resource-bundle:KeyUnkown")); } @Test public void testBadFormatBundleOnly() { final StrLookup lookup = new ResourceBundleLookup(); - Assert.assertNull(lookup.lookup("X")); + Assert.assertNull(lookup.lookup(null, "X")); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java index a91decb..9e7c3a6 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StrSubstitutorTest.java @@ -53,16 +53,16 @@ public class StrSubstitutorTest { final StrLookup lookup = new Interpolator(new MapLookup(map)); final StrSubstitutor subst = new StrSubstitutor(lookup); ThreadContext.put(TESTKEY, TESTVAL); - String value = subst.replace("${TestKey}-${ctx:TestKey}-${sys:TestKey}"); + String value = subst.replace(null, "${TestKey}-${ctx:TestKey}-${sys:TestKey}"); assertEquals("TestValue-TestValue-TestValue", value); - value = subst.replace("${BadKey}"); + value = subst.replace(null, "${BadKey}"); assertEquals("${BadKey}", value); - value = subst.replace("${BadKey:-Unknown}-${ctx:BadKey:-Unknown}-${sys:BadKey:-Unknown}"); + value = subst.replace(null, "${BadKey:-Unknown}-${ctx:BadKey:-Unknown}-${sys:BadKey:-Unknown}"); assertEquals("Unknown-Unknown-Unknown", value); - value = subst.replace("${BadKey:-Unknown}-${ctx:BadKey}-${sys:BadKey:-Unknown}"); + value = subst.replace(null, "${BadKey:-Unknown}-${ctx:BadKey}-${sys:BadKey:-Unknown}"); assertEquals("Unknown-${ctx:BadKey}-Unknown", value); - value = subst.replace("${BadKey:-Unknown}-${ctx:BadKey:-}-${sys:BadKey:-Unknown}"); + value = subst.replace(null, "${BadKey:-Unknown}-${ctx:BadKey:-}-${sys:BadKey:-Unknown}"); assertEquals("Unknown--Unknown", value); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java index 09081dd..bd093c1 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/StructuredDataLookupTest.java @@ -38,9 +38,9 @@ public class StructuredDataLookupTest { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(msg).build(); final StrLookup lookup = new StructuredDataLookup(); - String value = lookup.lookup(event, TESTKEY); + String value = lookup.lookup(null, event, TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("BadKey"); + value = lookup.lookup(null, "BadKey"); assertNull(value); } } diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java index 5749548..3bcdfb8 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/SystemPropertiesLookupTest.java @@ -43,9 +43,9 @@ public class SystemPropertiesLookupTest { @Test public void testLookup() { final StrLookup lookup = new SystemPropertiesLookup(); - String value = lookup.lookup(TESTKEY); + String value = lookup.lookup(null, TESTKEY); assertEquals(TESTVAL, value); - value = lookup.lookup("BadKey"); + value = lookup.lookup(null, "BadKey"); assertNull(value); } } diff --git a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/NoGcMessagePatternConverter.java b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/NoGcMessagePatternConverter.java index 4ecec89..88e3269 100644 --- a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/NoGcMessagePatternConverter.java +++ b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/nogc/NoGcMessagePatternConverter.java @@ -72,7 +72,7 @@ public final class NoGcMessagePatternConverter extends LogEventPatternConverter } if (result != null) { toAppendTo.append(config != null && result.contains("${") ? - config.getStrSubstitutor().replace(event, result) : result); + config.getStrSubstitutor().replace(config, event, result) : result); } else { toAppendTo.append("null"); } diff --git a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java index 471d2ba..a488991 100644 --- a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java +++ b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/CustomLookup.java @@ -20,6 +20,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.lookup.AbstractLookup; import org.apache.logging.log4j.core.lookup.StrLookup; @@ -49,7 +50,7 @@ public class CustomLookup extends AbstractLookup { * @return The value associated with the key. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { try { Map properties = loggerProperties.get(event.getLoggerName()); if (properties == null) { diff --git a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java index 1ed1868..be98010 100644 --- a/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java +++ b/log4j-samples/loggerProperties/src/main/java/org/apache/logging/log4j/lookup/MapMessageLookup.java @@ -20,6 +20,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.lookup.AbstractLookup; import org.apache.logging.log4j.core.lookup.StrLookup; @@ -50,7 +51,7 @@ public class MapMessageLookup extends AbstractLookup { * @return The value associated with the key. */ @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { Message msg = event.getMessage(); if (msg instanceof MapMessage) { try { diff --git a/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java b/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java index 2da549c..113ef70 100644 --- a/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java +++ b/log4j-web/src/main/java/org/apache/logging/log4j/web/Log4jWebInitializerImpl.java @@ -96,8 +96,8 @@ final class Log4jWebInitializerImpl extends AbstractLifeCycle implements Log4jWe if (this.isInitialized()) { super.setStarting(); - this.name = this.substitutor.replace(this.servletContext.getInitParameter(LOG4J_CONTEXT_NAME)); - final String location = this.substitutor.replace(this.servletContext + this.name = this.substitutor.replace(null, this.servletContext.getInitParameter(LOG4J_CONTEXT_NAME)); + final String location = this.substitutor.replace(null, this.servletContext .getInitParameter(LOG4J_CONFIG_LOCATION)); final boolean isJndi = "true".equalsIgnoreCase(this.servletContext .getInitParameter(IS_LOG4J_CONTEXT_SELECTOR_NAMED)); diff --git a/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java b/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java index 072f6c9..3cc0e78 100644 --- a/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java +++ b/log4j-web/src/main/java/org/apache/logging/log4j/web/WebLookup.java @@ -21,6 +21,7 @@ package org.apache.logging.log4j.web; import javax.servlet.ServletContext; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.lookup.AbstractLookup; import org.apache.logging.log4j.util.Strings; @@ -39,7 +40,7 @@ public class WebLookup extends AbstractLookup { } @Override - public String lookup(final LogEvent event, final String key) { + public String lookup(final Configuration config, final LogEvent event, final String key) { final ServletContext ctx = WebLoggerContextUtils.getServletContext(); if (ctx == null) { return null; diff --git a/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java b/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java index a93262e..f641d59 100644 --- a/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java +++ b/log4j-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java @@ -52,16 +52,16 @@ public class WebLookupTest { assertNotNull("No Configuration", config); final StrSubstitutor substitutor = config.getStrSubstitutor(); assertNotNull("No Interpolator", substitutor); - String value = substitutor.replace("${web:initParam.TestParam}"); + String value = substitutor.replace(null, "${web:initParam.TestParam}"); assertNotNull("No value for TestParam", value); assertEquals("Incorrect value for TestParam: " + value, "ParamValue", value); - value = substitutor.replace("${web:attr.TestAttr}"); + value = substitutor.replace(null, "${web:attr.TestAttr}"); assertNotNull("No value for TestAttr", value); assertEquals("Incorrect value for TestAttr: " + value, "AttrValue", value); - value = substitutor.replace("${web:Name1}"); + value = substitutor.replace(null, "${web:Name1}"); assertNotNull("No value for Name1", value); assertEquals("Incorrect value for Name1: " + value, "Ben", value); - value = substitutor.replace("${web:Name2}"); + value = substitutor.replace(null, "${web:Name2}"); assertNotNull("No value for Name2", value); assertEquals("Incorrect value for Name2: " + value, "Jerry", value); } catch (final IllegalStateException e) {