LOG4J2-1050 Fixed Log4jLookup.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/dec3a96c Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/dec3a96c Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/dec3a96c Branch: refs/heads/LOG4J2-1136 Commit: dec3a96cd719951f180f027343ac2ec715cca776 Parents: 8086bdc Author: rpopma <[email protected]> Authored: Sun Oct 4 02:22:27 2015 +0200 Committer: Ralph Goers <[email protected]> Committed: Sat Oct 3 23:08:07 2015 -0700 ---------------------------------------------------------------------- .../logging/log4j/core/lookup/Log4jLookup.java | 36 +++++++++++++++----- .../log4j/core/lookup/Log4jLookupTest.java | 21 ++++++++++-- .../core/lookup/Log4jLookupWithSpacesTest.java | 36 ++++++++++++++------ src/changes/changes.xml | 3 ++ 4 files changed, 73 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dec3a96c/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java ---------------------------------------------------------------------- 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 463fcb7..8ccb93f 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 @@ -15,11 +15,14 @@ */ package org.apache.logging.log4j.core.lookup; +import java.io.File; import java.net.URI; 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.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; @@ -57,20 +60,35 @@ public class Log4jLookup extends AbstractLookup { 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(); - switch (key) { - case KEY_CONFIG_LOCATION: - return asPath(ctx.getConfigLocation()); + case KEY_CONFIG_PARENT_LOCATION: + return file.getParentFile().getAbsolutePath(); - case KEY_CONFIG_PARENT_LOCATION: - try { - return asPath(getParent(ctx.getConfigLocation())); - } catch (final URISyntaxException use) { - LOGGER.error(use); + default: return null; } + } + + final URL url = configSrc.getURL(); + try { + switch (key) { + case KEY_CONFIG_LOCATION: + return asPath(url.toURI()); + + case KEY_CONFIG_PARENT_LOCATION: + return asPath(getParent(url.toURI())); - default: + default: + return null; + } + } catch (final URISyntaxException use) { + LOGGER.error(use); return null; } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dec3a96c/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java ---------------------------------------------------------------------- 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 1019aa6..c8a92a1 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 @@ -17,19 +17,25 @@ 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.URI; 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.ConfigurationSource; import org.apache.logging.log4j.core.impl.ContextAnchor; import org.easymock.EasyMock; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; /** @@ -37,15 +43,24 @@ import org.junit.Test; */ 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; @Before public void setup() throws URISyntaxException { this.mockCtx = EasyMock.createMock(LoggerContext.class); - expect(mockCtx.getConfigLocation()).andReturn(new URI("/a/b/c/d/e/log4j2.xml")); ContextAnchor.THREAD_CONTEXT.set(mockCtx); + final Configuration config = EasyMock.createMock(Configuration.class); + expect(mockCtx.getConfiguration()).andReturn(config); + + final ConfigurationSource configSrc = EasyMock.createMock(ConfigurationSource.class); + expect(config.getConfigurationSource()).andReturn(configSrc); + expect(configSrc.getFile()).andReturn(EXPECT); + replay(mockCtx); + replay(config); + replay(configSrc); } @After @@ -60,13 +75,13 @@ public class Log4jLookupTest { public void lookupConfigLocation() { final StrLookup log4jLookup = new Log4jLookup(); final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); - assertEquals("/a/b/c/d/e/log4j2.xml", value); + assertEquals(EXPECT.getAbsolutePath(), value); } @Test public void lookupConfigParentLocation() { final StrLookup log4jLookup = new Log4jLookup(); final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); - assertEquals("/a/b/c/d/e", value); + assertEquals(EXPECT.getParentFile().getAbsolutePath(), value); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dec3a96c/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java ---------------------------------------------------------------------- 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 58628b7..fb938cd 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 @@ -15,36 +15,48 @@ */ 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.MalformedURLException; +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.ConfigurationSource; import org.apache.logging.log4j.core.impl.ContextAnchor; import org.easymock.EasyMock; import org.junit.After; import org.junit.Before; import org.junit.Test; +import static org.apache.logging.log4j.core.lookup.Log4jLookup.*; + +import static org.easymock.EasyMock.*; + +import static org.junit.Assert.*; + /** * */ 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; @Before - public void setup() { + public void setup() throws URISyntaxException, MalformedURLException { this.mockCtx = EasyMock.createMock(LoggerContext.class); - expect(mockCtx.getConfigLocation()).andReturn(new File("/a a/b b/c c/d d/e e/log4j2 file.xml").toURI()); ContextAnchor.THREAD_CONTEXT.set(mockCtx); + final Configuration config = EasyMock.createMock(Configuration.class); + expect(mockCtx.getConfiguration()).andReturn(config); + + final ConfigurationSource configSrc = EasyMock.createMock(ConfigurationSource.class); + expect(config.getConfigurationSource()).andReturn(configSrc); + expect(configSrc.getFile()).andReturn(EXPECT); + replay(mockCtx); + replay(config); + replay(configSrc); } @After @@ -59,13 +71,15 @@ public class Log4jLookupWithSpacesTest { public void lookupConfigLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); final String value = log4jLookup.lookup(KEY_CONFIG_LOCATION); - assertEquals(new File("/a a/b b/c c/d d/e e/log4j2 file.xml").toURI().getPath(), value); + assertEquals( + new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e/log4j2 file.xml").getAbsolutePath(), + value); } @Test public void lookupConfigParentLocation_withSpaces() { final StrLookup log4jLookup = new Log4jLookup(); final String value = log4jLookup.lookup(KEY_CONFIG_PARENT_LOCATION); - assertEquals(new File("/a a/b b/c c/d d/e e").toURI().getPath(), value); + assertEquals(new File(System.getProperty("user.home"), "/a a/b b/c c/d d/e e").getAbsolutePath(), value); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/dec3a96c/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d6fad6e..7b9c7a7 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -39,6 +39,9 @@ <action issue="LOG4J2-1020" dev="mikes" type="add"> Add possibility to set shutdown timeout on AsyncAppender. </action> + <action issue="LOG4J2-1050" dev="rpopma" type="fix"> + Fixed Log4jLookup. + </action> <action issue="LOG4J2-1142" dev="rpopma" type="fix"> Fix potential memory leak in web applications by using a straight ThreadLocal field instead of subclassing ThreadLocal. </action>
