diff --git a/cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverter.java b/cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverter.java index c2a8b773..e51ae52d 100644 --- a/cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverter.java +++ b/cf-java-logging-support-log4j2/src/main/java/com/sap/hcp/cf/log4j2/converter/ContextPropsConverter.java @@ -1,6 +1,7 @@ package com.sap.hcp.cf.log4j2.converter; import java.util.Arrays; +import java.util.HashMap; import java.util.Map; import org.apache.logging.log4j.core.LogEvent; @@ -47,30 +48,38 @@ public static ContextPropsConverter newInstance(final String[] options) { @Override public void format(LogEvent event, StringBuilder toAppendTo) { - Map contextData = event.getContextData().toMap(); - addCustomFieldsFromArguments(contextData, event); + Map contextData = event.getContextData().toMap(); + contextData = addCustomFieldsFromArguments(contextData, event); int lengthBefore = toAppendTo.length(); - converter.convert(toAppendTo, contextData); + converter.convert(toAppendTo, contextData); // remove comma from pattern, when no properties are added // this is to avoid a double comma in the JSON // Do not do this on empty messages if (toAppendTo.length() == lengthBefore && lengthBefore > 0 && toAppendTo.charAt(lengthBefore - 1) == ',') { toAppendTo.setLength(lengthBefore - 1); } - } + } - private void addCustomFieldsFromArguments(Map contextData, LogEvent event) { - Message message = event.getMessage(); - Object[] parameters = message.getParameters(); - if (parameters == null) { - return; - } - for (Object current : parameters) { - if (current instanceof CustomField) { - CustomField field = (CustomField) current; - contextData.put(field.getKey(), field.getValue()); - } - } - } + private Map addCustomFieldsFromArguments(Map contextData, LogEvent event) { + Message message = event.getMessage(); + Object[] parameters = message.getParameters(); + if (parameters == null) { + return contextData; + } + boolean unchangedContextData = true; + Map result = contextData; + for (Object current: parameters) { + if (current instanceof CustomField) { + CustomField field = (CustomField) current; + if (unchangedContextData) { + // contextData might be an unmodifiable map + result = new HashMap<>(contextData); + unchangedContextData = false; + } + result.put(field.getKey(), field.getValue()); + } + } + return result; + } }