Index: log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolation.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolation.java (revision ) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolation.java (revision ) @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core.lookup; + +/** + * Helper class returning partial lookups. + */ +class Interpolation { + final StrLookup lookup; + final String prefix; + final String variableName; + + public Interpolation(final StrLookup lookup, final String prefix, final String variableName) { + this.lookup = lookup; + this.prefix = prefix; + this.variableName = variableName; + } +} Index: log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java (revision 19ac9876f81629a318fad85ce5d795e059ba479b) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java (revision ) @@ -942,6 +942,8 @@ pos += endMatchLen; final int endPos = pos; + final Interpolation interpolation = interpolate(varNameExpr); + varNameExpr = interpolation.variableName; String varName = varNameExpr; String varDefaultValue = null; @@ -973,7 +975,11 @@ priorVariables.add(varName); // resolve the variable - String varValue = resolveVariable(event, varName, buf, startPos, endPos); + final StrLookup lookup = interpolation.lookup; + String varValue = null; + if (lookup != null) { + varValue = lookup.lookup(event, varName); + } if (varValue == null) { varValue = varDefaultValue; } @@ -1025,33 +1031,23 @@ throw new IllegalStateException(buf.toString()); } + private Interpolation interpolate(final String varNameExpr) { + if (variableResolver instanceof Interpolator) { + return ((Interpolator) variableResolver).interpolate(varNameExpr); + } + return new Interpolation(null, null, varNameExpr); + } + /** - * Internal method that resolves the value of a variable. - *
- * Most users of this class do not need to call this method. This method is - * called automatically by the substitution process. - *
- *- * Writers of subclasses can override this method if they need to alter - * how each substitution occurs. The method is passed the variable's name - * and must return the corresponding value. This implementation uses the - * {@link #getVariableResolver()} with the variable's name as the key. - *
+ * This method is never called and does nothing. (It only exists to preserve binary compatibility with earlier + * versions of this class.) * - * @param event The LogEvent, if there is one. - * @param variableName the name of the variable, not null - * @param buf the buffer where the substitution is occurring, not null - * @param startPos the start position of the variable including the prefix, valid - * @param endPos the end position of the variable including the suffix, valid - * @return the variable's value or null if the variable is unknown + * @deprecated with no replacement */ + @Deprecated protected String resolveVariable(final LogEvent event, final String variableName, final StringBuilder buf, - final int startPos, final int endPos) { + final int startPos, final int endPos) { - final StrLookup resolver = getVariableResolver(); - if (resolver == null) { - return null; + return null; - } - return resolver.lookup(event, variableName); } // Escape Index: log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java (revision 19ac9876f81629a318fad85ce5d795e059ba479b) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java (revision ) @@ -210,4 +210,22 @@ } return sb.toString(); } + + Interpolation interpolate(final String var) { + if (var == null) { + return null; + } + + final int prefixPos = var.indexOf(PREFIX_SEPARATOR); + if (prefixPos >= 0) { + final String prefix = var.substring(0, prefixPos); + final String name = var.substring(prefixPos + 1); + final StrLookup lookup = lookups.get(prefix); + if (lookup instanceof ConfigurationAware) { + ((ConfigurationAware) lookup).setConfiguration(configuration); + } + return new Interpolation(lookup, prefix, name); + } + return new Interpolation(defaultLookup, null, var); + } }