Repository: logging-log4j2 Updated Branches: refs/heads/master cf67e8aa7 -> b2927d288
[LOG4J2-1013] Log4j2 Main Arguments Lookup by name doesn't work. Make the main map lookup a first class citizen by turning it into a plug in. Start up will now properly pick it up. The bug in the ticket is not solved but it is now easier to test. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/b2927d28 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/b2927d28 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/b2927d28 Branch: refs/heads/master Commit: b2927d2884010925129eb8dbc89f405277cf9f0e Parents: cf67e8a Author: Gary Gregory <[email protected]> Authored: Mon May 18 21:24:51 2015 -0700 Committer: Gary Gregory <[email protected]> Committed: Mon May 18 21:24:51 2015 -0700 ---------------------------------------------------------------------- .../logging/log4j/core/lookup/Interpolator.java | 2 +- .../log4j/core/lookup/MainMapLookup.java | 75 ++++++++++++++++++++ .../logging/log4j/core/lookup/MapLookup.java | 18 +++-- .../lookup/MainInputArgumentsLookupTest.java | 44 ++++++++++++ .../log4j/core/lookup/MapLookupTest.java | 2 +- .../src/test/resources/log4j-lookup-main.xml | 31 ++++++++ src/site/xdoc/manual/lookups.xml | 4 +- 7 files changed, 162 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/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 6b697ff..3dda892 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 @@ -84,7 +84,7 @@ public class Interpolator extends AbstractLookup { // TODO: this ought to use the PluginManager lookups.put("sys", new SystemPropertiesLookup()); lookups.put("env", new EnvironmentLookup()); - lookups.put("main", MapLookup.MAIN_SINGLETON); + lookups.put("main", MainMapLookup.MAIN_SINGLETON); lookups.put("java", new JavaLookup()); // JNDI try { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..ec632b7 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MainMapLookup.java @@ -0,0 +1,75 @@ +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.plugins.Plugin; + +/** + * A map-based lookup for main arguments. + * + * See {@link #setMainArguments(String[])}. + * + * @since 2.4 + */ +@Plugin(name = "main", category = StrLookup.CATEGORY) +public class MainMapLookup extends MapLookup { + + /** + * A singleton used by a main method to save its arguments. + */ + static final MapLookup MAIN_SINGLETON = new MapLookup(MapLookup.newMap(0)); + + /** + * An application's {@code public static main(String[])} method calls this method to make its main arguments + * available for lookup with the prefix {@code main}. + * <p> + * The map provides two kinds of access: First by index, starting at {@code "0"}, {@code "1"} and so on. For + * example, the command line {@code --file path/file.txt -x 2} can be accessed from a configuration file with: + * </p> + * <ul> + * <li>{@code "main:0"} = {@code "--file"}</li> + * <li>{@code "main:1"} = {@code "path/file.txt"}</li> + * <li>{@code "main:2"} = {@code "-x"}</li> + * <li>{@code "main:3"} = {@code "2"}</li> + * </ul> + * <p> + * Second using the argument at position n as the key to access the value at n+1. + * </p> + * <ul> + * <li>{@code "main:--file"} = {@code "path/file.txt"}</li> + * <li>{@code "main:-x"} = {@code "2"}</li> + * </ul> + * + * @param args + * An application's {@code public static main(String[])} arguments. + */ + public static void setMainArguments(final String[] args) { + if (args == null) { + return; + } + initMap(args, MainMapLookup.MAIN_SINGLETON.getMap()); + } + + /** + * Constructor when used directly as a plugin. + */ + public MainMapLookup() { + // no-init + } + + public MainMapLookup(final Map<String, String> map) { + super(map); + } + + @Override + public String lookup(LogEvent event, String key) { + return MAIN_SINGLETON.getMap().get(key); + } + + @Override + public String lookup(String key) { + return MAIN_SINGLETON.getMap().get(key); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/MapLookup.java ---------------------------------------------------------------------- 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 ad9026d..ef8a776 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 @@ -30,11 +30,6 @@ import org.apache.logging.log4j.message.MapMessage; @Plugin(name = "map", category = StrLookup.CATEGORY) public class MapLookup implements StrLookup { - /** - * A singleton used by a main method to save its arguments. - */ - static final MapLookup MAIN_SINGLETON = new MapLookup(newMap(0)); - static Map<String, String> initMap(final String[] srcArgs, final Map<String, String> destMap) { for (int i = 0; i < srcArgs.length; i++) { final int next = i + 1; @@ -45,7 +40,7 @@ public class MapLookup implements StrLookup { return destMap; } - private static HashMap<String, String> newMap(final int initialCapacity) { + static HashMap<String, String> newMap(final int initialCapacity) { return new HashMap<>(initialCapacity); } @@ -73,12 +68,11 @@ public class MapLookup implements StrLookup { * @param args * An application's {@code public static main(String[])} arguments. * @since 2.1 + * @deprecated As of 2.4, use {@link MainMapLookup#setMainArguments(String[])} */ + @Deprecated public static void setMainArguments(final String[] args) { - if (args == null) { - return; - } - initMap(args, MAIN_SINGLETON.map); + MainMapLookup.setMainArguments(args); } static Map<String, String> toMap(final List<String> args) { @@ -118,6 +112,10 @@ public class MapLookup implements StrLookup { this.map = map; } + protected Map<String, String> getMap() { + return map; + } + @Override public String lookup(final LogEvent event, final String key) { if (map == null && !(event.getMessage() instanceof MapMessage)) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsLookupTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsLookupTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsLookupTest.java new file mode 100644 index 0000000..d8c32b7 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MainInputArgumentsLookupTest.java @@ -0,0 +1,44 @@ +/* + * 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; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configurator; + +/** + * Tests {@link org.apache.logging.log4j.core.lookup.MainMapLookup#MAIN_SINGLETON} from the command line, not a real JUnit + * test. + * + * From an IDE or CLI: --file foo.txt + * + * @since 2.4 + */ +public class MainInputArgumentsLookupTest { + + public static void main(final String[] args) { + MainMapLookup.setMainArguments(args); + final LoggerContext ctx = Configurator.initialize(MainInputArgumentsLookupTest.class.getName(), + "target/test-classes/log4j-lookup-main.xml"); + try { + LogManager.getLogger().error("this is an error message"); + } finally { + Configurator.shutdown(ctx); + } + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/MapLookupTest.java ---------------------------------------------------------------------- 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 4a34d60..be550e3 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 @@ -55,7 +55,7 @@ public class MapLookupTest { MapLookup.setMainArguments(new String[] { "--file", "foo.txt" }); - final MapLookup lookup = MapLookup.MAIN_SINGLETON; + final MapLookup lookup = MainMapLookup.MAIN_SINGLETON; assertEquals(null, lookup.lookup(null)); assertEquals(null, lookup.lookup("X")); assertEquals("--file", lookup.lookup("0")); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/log4j-core/src/test/resources/log4j-lookup-main.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/log4j-lookup-main.xml b/log4j-core/src/test/resources/log4j-lookup-main.xml new file mode 100644 index 0000000..85e4999 --- /dev/null +++ b/log4j-core/src/test/resources/log4j-lookup-main.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. + +--> +<Configuration status="OFF"> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d [%t] [${main:1}][${main:-1}][${main:--file}] %-5level: %msg%n%throwable" /> + </Console> + </Appenders> + <Loggers> + <Logger name="org.foo" level="DEBUG" /> + <Root level="TRACE"> + <AppenderRef ref="Console" /> + </Root> + </Loggers> +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b2927d28/src/site/xdoc/manual/lookups.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/lookups.xml b/src/site/xdoc/manual/lookups.xml index 5137e37..e209e2a 100644 --- a/src/site/xdoc/manual/lookups.xml +++ b/src/site/xdoc/manual/lookups.xml @@ -183,10 +183,10 @@ the main arguments of the application to Log4j: </p> <pre class="prettyprint linenums"><![CDATA[ -import org.apache.logging.log4j.core.lookup.MapLookup; +import org.apache.logging.log4j.core.lookup.MainMapLookup; public static void main(String args[]) { - MapLookup.setMainArguments(args); + MainMapLookup.setMainArguments(args); ... }]]></pre> <p>
