SAP / cf-java-logging-support

The Java Logging Support for Cloud Foundry supports the creation of structured log messages and the collection of request metrics
Apache License 2.0
77 stars 48 forks source link

Custom Fields might lead to "java.lang.UnsupportedOperationException" with log4j2 #135

Closed KarstenSchnitter closed 2 years ago

KarstenSchnitter commented 2 years ago

Custom fields can be added to log messages by using the CustomField class:

LOGGER.info("Hello World!", CustomField.customField("my_field", "my_value"));

This can result in the following stacktrace:

2022-01-21 09:59:49,158 main ERROR An exception occurred processing Appender Console java.lang.UnsupportedOperationException
    at java.base/java.util.Collections$UnmodifiableMap.put(Collections.java:1457)
    at com.sap.hcp.cf.log4j2.converter.ContextPropsConverter.addCustomFieldsFromArguments(ContextPropsConverter.java:63)
    at com.sap.hcp.cf.log4j2.converter.ContextPropsConverter.format(ContextPropsConverter.java:50)
    at org.apache.logging.log4j.core.pattern.PatternFormatter.format(PatternFormatter.java:44)
    at com.sap.hcp.cf.log4j2.layout.JsonPatternLayout.toSerializable(JsonPatternLayout.java:47)
    at com.sap.hcp.cf.log4j2.layout.JsonPatternLayout.toSerializable(JsonPatternLayout.java:23)
    at org.apache.logging.log4j.core.layout.AbstractStringLayout.toByteArray(AbstractStringLayout.java:282)
    at org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:209)
    at org.apache.logging.log4j.core.layout.AbstractLayout.encode(AbstractLayout.java:37)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.directEncodeEvent(AbstractOutputStreamAppender.java:197)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.tryAppend(AbstractOutputStreamAppender.java:190)
    at org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender.append(AbstractOutputStreamAppender.java:181)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:161)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:134)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:125)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:89)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:542)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:500)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:483)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:417)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:82)
    at org.apache.logging.log4j.core.Logger.log(Logger.java:161)
    at org.apache.logging.log4j.spi.AbstractLogger.tryLogMessage(AbstractLogger.java:2205)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageTrackRecursion(AbstractLogger.java:2159)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely(AbstractLogger.java:2142)
    at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:2034)
    at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1899)
    at org.apache.logging.slf4j.Log4jLogger.error(Log4jLogger.java:304)

The current implementation of com.sap.hcp.cf.log4j2.converter.ContextPropsConverter.addCustomFieldsFromArgumentsdoes not reflect, that event.getContextData().toMap() might return an unmodifiable map. This needs to be changed to avoid that error.