Repository: logging-log4j2 Updated Branches: refs/heads/master 42adb50bc -> 278398ded
[LOG4J2-2177] Replace use of deprecated Core API org.apache.logging.log4j.core.impl.Log4jLogEvent.Builder.setContextMap(Map<String, String>) Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/278398de Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/278398de Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/278398de Branch: refs/heads/master Commit: 278398deddc17ec7fb596d4ee53a9f91f5c1ac02 Parents: 42adb50 Author: Gary Gregory <[email protected]> Authored: Thu Jan 4 13:35:37 2018 -0700 Committer: Gary Gregory <[email protected]> Committed: Thu Jan 4 13:35:37 2018 -0700 ---------------------------------------------------------------------- .../org/apache/logging/log4j/util/SortedArrayStringMap.java | 4 ++-- .../apache/logging/log4j/util/SortedArrayStringMapTest.java | 6 +++--- .../core/appender/rewrite/PropertiesRewritePolicy.java | 9 ++++++--- .../apache/logging/log4j/core/impl/ContextDataFactory.java | 8 ++++++++ 4 files changed, 19 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278398de/log4j-api/src/main/java/org/apache/logging/log4j/util/SortedArrayStringMap.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/SortedArrayStringMap.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/SortedArrayStringMap.java index b54dc1b..05739f2 100644 --- a/log4j-api/src/main/java/org/apache/logging/log4j/util/SortedArrayStringMap.java +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/SortedArrayStringMap.java @@ -138,10 +138,10 @@ public class SortedArrayStringMap implements IndexedStringMap { } public SortedArrayStringMap(final int initialCapacity) { - if (initialCapacity < 1) { + if (initialCapacity < 0) { throw new IllegalArgumentException("Initial capacity must be at least one but was " + initialCapacity); } - threshold = ceilingNextPowerOfTwo(initialCapacity); + threshold = ceilingNextPowerOfTwo(initialCapacity == 0 ? 1 : initialCapacity); } public SortedArrayStringMap(final ReadOnlyStringMap other) { http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278398de/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java index d97b7e2..27af220 100644 --- a/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java +++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/SortedArrayStringMapTest.java @@ -47,9 +47,9 @@ public class SortedArrayStringMapTest { new SortedArrayStringMap(-1); } - @Test(expected = IllegalArgumentException.class) - public void testConstructorDisallowsZeroCapacity() throws Exception { - new SortedArrayStringMap(0); + public void testConstructorAllowsZeroCapacity() throws Exception { + SortedArrayStringMap sortedArrayStringMap = new SortedArrayStringMap(0); + assertEquals(0, sortedArrayStringMap.size()); } @Test http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278398de/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java index 0638970..5d52851 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rewrite/PropertiesRewritePolicy.java @@ -30,8 +30,11 @@ import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.PluginFactory; +import org.apache.logging.log4j.core.impl.ContextDataFactory; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.ReadOnlyStringMap; +import org.apache.logging.log4j.util.StringMap; /** * This policy modifies events by replacing or possibly adding keys and values to the MapMessage. @@ -65,14 +68,14 @@ public final class PropertiesRewritePolicy implements RewritePolicy { */ @Override public LogEvent rewrite(final LogEvent source) { - final Map<String, String> props = new HashMap<>(source.getContextData().toMap()); + final StringMap newContextData = ContextDataFactory.createContextData(source.getContextData()); for (final Map.Entry<Property, Boolean> entry : properties.entrySet()) { final Property prop = entry.getKey(); - props.put(prop.getName(), entry.getValue().booleanValue() ? + newContextData.putValue(prop.getName(), entry.getValue().booleanValue() ? config.getStrSubstitutor().replace(prop.getValue()) : prop.getValue()); } - return new Log4jLogEvent.Builder(source).setContextMap(props).build(); + return new Log4jLogEvent.Builder(source).setContextData(newContextData).build(); } @Override http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278398de/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java index 8abc22a..1dba9d2 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java @@ -24,6 +24,7 @@ import org.apache.logging.log4j.core.ContextDataInjector; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.util.LoaderUtil; import org.apache.logging.log4j.util.PropertiesUtil; +import org.apache.logging.log4j.util.ReadOnlyStringMap; import org.apache.logging.log4j.util.SortedArrayStringMap; import org.apache.logging.log4j.util.StringMap; @@ -111,6 +112,12 @@ public class ContextDataFactory { } } + public static StringMap createContextData(final ReadOnlyStringMap readOnlyStringMap) { + final StringMap contextData = createContextData(readOnlyStringMap.size()); + contextData.putAll(readOnlyStringMap); + return contextData; + } + /** * An empty pre-frozen StringMap. The returned object may be shared. * @@ -119,4 +126,5 @@ public class ContextDataFactory { public static StringMap emptyFrozenContextData() { return EMPTY_STRING_MAP; } + }
