You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2015/06/13 23:24:13 UTC

logging-log4j2 git commit: [LOG4J2-1050] Add a Log4jLookup class to help write log files relative to log4j2.xml.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master b4fe986a3 -> e63fd835f


[LOG4J2-1050] Add a Log4jLookup class to help write log files relative
to log4j2.xml.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/e63fd835
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/e63fd835
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/e63fd835

Branch: refs/heads/master
Commit: e63fd835fc3556a9996850051c56b7aac2331e9c
Parents: b4fe986
Author: Gary Gregory <ga...@gmail.com>
Authored: Sat Jun 13 14:24:07 2015 -0700
Committer: Gary Gregory <ga...@gmail.com>
Committed: Sat Jun 13 14:24:07 2015 -0700

----------------------------------------------------------------------
 .../logging/log4j/core/lookup/Interpolator.java |  1 +
 .../logging/log4j/core/lookup/Log4jLookup.java  | 77 ++++++++++++++++++++
 .../log4j/core/lookup/Log4jLookupTest.java      | 72 ++++++++++++++++++
 .../core/lookup/Log4jLookupWithSpacesTest.java  | 71 ++++++++++++++++++
 src/changes/changes.xml                         |  3 +
 src/site/xdoc/manual/configuration.xml.vm       |  4 +
 6 files changed, 228 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e63fd835/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
----------------------------------------------------------------------
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 de0a466..ec2fbc0 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
@@ -82,6 +82,7 @@ public class Interpolator extends AbstractLookup {
     public Interpolator(final Map<String, String> properties) {
         this.defaultLookup = new MapLookup(properties == null ? new HashMap<String, String>() : properties);
         // TODO: this ought to use the PluginManager
+        lookups.put("log4j", new Log4jLookup());
         lookups.put("sys", new SystemPropertiesLookup());
         lookups.put("env", new EnvironmentLookup());
         lookups.put("main", MainMapLookup.MAIN_SINGLETON);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e63fd835/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
new file mode 100644
index 0000000..463fcb7
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Log4jLookup.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2015 Apache Software Foundation.
+ *
+ * Licensed 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;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+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 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 static String asPath(final URI uri) {
+        if (uri.getScheme() == null || uri.getScheme().equals("file")) {
+            return uri.getPath();
+        }
+        return uri.toString();
+    }
+
+    private static URI getParent(final URI uri) throws URISyntaxException {
+        final String s = uri.toString();
+        final int offset = s.lastIndexOf('/');
+        if (offset > -1) {
+            return new URI(s.substring(0, offset));
+        }
+        return new URI("../");
+    }
+
+    @Override
+    public String lookup(final LogEvent event, final String key) {
+        final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
+        if (ctx == null) {
+            return null;
+        }
+
+        switch (key) {
+        case KEY_CONFIG_LOCATION:
+            return asPath(ctx.getConfigLocation());
+
+        case KEY_CONFIG_PARENT_LOCATION:
+            try {
+                return asPath(getParent(ctx.getConfigLocation()));
+            } catch (final URISyntaxException use) {
+                LOGGER.error(use);
+                return null;
+            }
+
+        default:
+            return null;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e63fd835/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
new file mode 100644
index 0000000..1019aa6
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2015 Apache Software Foundation.
+ *
+ * Licensed 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;
+
+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.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class Log4jLookupTest {
+
+    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);
+
+        replay(mockCtx);
+    }
+
+    @After
+    public void cleanup() {
+        verify(mockCtx);
+
+        ContextAnchor.THREAD_CONTEXT.set(null);
+        this.mockCtx = null;
+    }
+
+    @Test
+    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);
+    }
+
+    @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);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e63fd835/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
new file mode 100644
index 0000000..58628b7
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/Log4jLookupWithSpacesTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2015 Apache Software Foundation.
+ *
+ * Licensed 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;
+
+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 org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.impl.ContextAnchor;
+import org.easymock.EasyMock;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class Log4jLookupWithSpacesTest {
+
+    private LoggerContext mockCtx = null;
+
+    @Before
+    public void setup() {
+        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);
+
+        replay(mockCtx);
+    }
+
+    @After
+    public void cleanup() {
+        verify(mockCtx);
+
+        ContextAnchor.THREAD_CONTEXT.set(null);
+        this.mockCtx = null;
+    }
+
+    @Test
+    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);
+    }
+
+    @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);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e63fd835/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7f9aca8..849457b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -33,6 +33,9 @@
       <action issue="LOG4J2-1015" dev="ggregory" type="add" due-to="Daniel Marcotte">
         Add a way to route messages based on the %marker in Layout for RoutingAppender.
       </action>
+      <action issue="LOG4J2-1050" dev="ggregory" type="add" due-to="Adam Retter">
+        Add a Log4jLookup class to help write log files relative to log4j2.xml.
+      </action>
       <action issue="LOG4J2-1051" dev="ggregory" type="fix" due-to="Lukasz Lenart">
         NoClassDefFoundError when starting app on Google App Engine.
       </action>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e63fd835/src/site/xdoc/manual/configuration.xml.vm
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/configuration.xml.vm b/src/site/xdoc/manual/configuration.xml.vm
index 8015054..30f0f00 100644
--- a/src/site/xdoc/manual/configuration.xml.vm
+++ b/src/site/xdoc/manual/configuration.xml.vm
@@ -904,6 +904,10 @@ public class Bar {
                   Not available on Android.</td>
               </tr>
               <tr>
+                <td>log4j</td>
+                <td>Log4j configuration properties. The expressions <code>${log4j:configLocation}</code> and <code>${log4j:configParentLocation}</code> respectively provide the absolute path to the log4j configuration file and its parent folder.</td>
+              </tr>
+              <tr>
                 <td>main</td>
                 <td>A value set with <a class="javadoc" href="../log4j-core/apidocs/org/apache/logging/log4j/core/lookup/MapLookup.html#setMainArguments-java.lang.String:A-">MapLookup.setMainArguments(String[])</a></td>
               </tr>