Repository: logging-log4j2 Updated Branches: refs/heads/master 713f1a9ba -> 006706615
LOG4J2-1010 bugfix in ThreadContextDataInjector.ForCopyOnWriteThreadContext: prevent potential UnsupportedOperationException when using async loggers and some loggers have properties configured and some don't Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/281ad99a Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/281ad99a Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/281ad99a Branch: refs/heads/master Commit: 281ad99a011b1952c721e5f3063fc1dc639f1716 Parents: 713f1a9 Author: rpopma <rpo...@apache.org> Authored: Fri Sep 23 17:48:46 2016 +0900 Committer: rpopma <rpo...@apache.org> Committed: Fri Sep 23 17:48:46 2016 +0900 ---------------------------------------------------------------------- .../log4j/core/impl/ThreadContextDataInjector.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/281ad99a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java index d26324c..92144c4 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ThreadContextDataInjector.java @@ -144,22 +144,25 @@ public class ThreadContextDataInjector { * specified reusable StringMap. * * @param props list of configuration properties, may be {@code null} - * @param reusable a {@code StringMap} instance that may be reused to avoid creating temporary objects + * @param ignore a {@code StringMap} instance from the log event * @return a {@code StringMap} combining configuration properties with thread context data */ @Override - public StringMap injectContextData(final List<Property> props, final StringMap reusable) { + public StringMap injectContextData(final List<Property> props, final StringMap ignore) { // If there are no configuration properties we want to just return the ThreadContext's StringMap: // it is a copy-on-write data structure so we are sure ThreadContext changes will not affect our copy. final StringMap immutableCopy = ThreadContextAccess.getThreadContextMap2().getReadOnlyContextData(); if (props == null || props.isEmpty()) { - return immutableCopy; + return immutableCopy; // this will replace the LogEvent's context data with the returned instance } // However, if the list of Properties is non-empty we need to combine the properties and the ThreadContext - // data. In that case we will copy the key-value pairs into the specified reusable StringMap. - copyProperties(props, reusable); - reusable.putAll(immutableCopy); - return reusable; + // data. Note that we cannot reuse the specified StringMap: some Loggers may have properties defined + // and others not, so the LogEvent's context data may have been replaced with an immutable copy from + // the ThreadContext - this will throw an UnsupportedOperationException if we try to modify it. + final StringMap result = ContextDataFactory.createContextData(); + copyProperties(props, result); + result.putAll(immutableCopy); + return result; } @Override