Index: log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.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/Log4jLookup.java (revision 287102c2cd533353730cb4d7fc2c0c17b6639d75) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java (revision ) @@ -22,23 +22,25 @@ 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.ConfigurationAware; 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; /** * Lookup properties of Log4j */ @Plugin(name = "log4j", category = StrLookup.CATEGORY) -public class Log4jLookup extends AbstractLookup { +public class Log4jLookup extends AbstractLookup implements ConfigurationAware { public final static String KEY_CONFIG_LOCATION = "configLocation"; public final static String KEY_CONFIG_PARENT_LOCATION = "configParentLocation"; private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); + private Configuration config; + private static String asPath(final URI uri) { if (uri.getScheme() == null || uri.getScheme().equals("file")) { return uri.getPath(); @@ -57,40 +59,47 @@ @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(); + 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(); + 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(); + final URL url = configSrc.getURL(); + if (url != null) { - try { - switch (key) { - case KEY_CONFIG_LOCATION: - return asPath(url.toURI()); + 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; - } - } catch (final URISyntaxException use) { - LOGGER.error(use); - return null; - } + default: + return null; + } + } catch (final URISyntaxException use) { + LOGGER.error(use); + return null; + } + } + } + + return null; + } + + @Override + public void setConfiguration(final Configuration configuration) { + config = configuration; } } 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 287102c2cd533353730cb4d7fc2c0c17b6639d75) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/StrSubstitutor.java (revision ) @@ -25,6 +25,8 @@ import java.util.Properties; import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.util.Strings; /** @@ -136,7 +138,7 @@ * property to true. *

*/ -public class StrSubstitutor { +public class StrSubstitutor implements ConfigurationAware { /** * Constant for the default escape character. @@ -184,6 +186,7 @@ * The flag whether substitution in variable names is enabled. */ private boolean enableSubstitutionInVariables; + private Configuration configuration; //----------------------------------------------------------------------- /** @@ -1294,6 +1297,9 @@ * @param variableResolver the VariableResolver */ public void setVariableResolver(final StrLookup variableResolver) { + if (variableResolver instanceof ConfigurationAware && this.configuration != null) { + ((ConfigurationAware) variableResolver).setConfiguration(this.configuration); + } this.variableResolver = variableResolver; } @@ -1351,5 +1357,13 @@ @Override public String toString() { return "StrSubstitutor(" + variableResolver.toString() + ')'; + } + + @Override + public void setConfiguration(final Configuration configuration) { + this.configuration = configuration; + if (this.variableResolver instanceof ConfigurationAware) { + ((ConfigurationAware) this.variableResolver).setConfiguration(this.configuration); + } } } Index: log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java (revision 287102c2cd533353730cb4d7fc2c0c17b6639d75) +++ log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java (revision ) @@ -15,20 +15,12 @@ */ package org.apache.logging.log4j.core.lookup; -import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_LOCATION; -import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_PARENT_LOCATION; - -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; - -import static org.junit.Assert.assertEquals; - import java.io.File; import java.net.URISyntaxException; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.impl.ContextAnchor; import org.easymock.EasyMock; @@ -36,6 +28,13 @@ import org.junit.Before; import org.junit.Test; +import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_LOCATION; +import static org.apache.logging.log4j.core.lookup.Log4jLookup.KEY_CONFIG_PARENT_LOCATION; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.junit.Assert.*; + /** * */ @@ -43,27 +42,26 @@ 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,6 +70,7 @@ @Test public void lookupConfigLocation() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); assertEquals(EXPECT.getAbsolutePath(), value); } @@ -79,6 +78,7 @@ @Test public void lookupConfigParentLocation() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); assertEquals(EXPECT.getParentFile().getAbsolutePath(), value); } Index: log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java (revision 287102c2cd533353730cb4d7fc2c0c17b6639d75) +++ log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java (revision ) @@ -21,6 +21,7 @@ import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationAware; import org.apache.logging.log4j.core.config.ConfigurationSource; import org.apache.logging.log4j.core.impl.ContextAnchor; import org.easymock.EasyMock; @@ -41,27 +42,26 @@ 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,6 +70,7 @@ @Test public void lookupConfigLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); assertEquals( new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e/log4j2 file.xml").getAbsolutePath(), @@ -79,6 +80,7 @@ @Test public void lookupConfigParentLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); + ((ConfigurationAware) log4jLookup).setConfiguration(config); final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); assertEquals(new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e").getAbsolutePath(), value); } Index: log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java (revision 287102c2cd533353730cb4d7fc2c0c17b6639d75) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java (revision ) @@ -177,6 +177,7 @@ @Override public void initialize() { LOGGER.debug("Initializing configuration {}", this); + subst.setConfiguration(this); scriptManager = new ScriptManager(watchManager); pluginManager.collectPlugins(pluginPackages); final PluginManager levelPlugins = new PluginManager(Level.CATEGORY); Index: log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java (revision ) +++ log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationAware.java (revision ) @@ -0,0 +1,26 @@ +/* + * 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.config; + +/** + * + */ +public interface ConfigurationAware { + + void setConfiguration(Configuration configuration); +}