Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KarstenSchnitter opened this issue Jan 21, 2022 · 0 comments · Fixed by #136
Closed
Labels
good-first-issue This issue is easy to implement. It can be taken to get into contributing ot the library. log4j This issue is concerned with the log4j integration.

Comments

@KarstenSchnitter
Copy link
Contributor

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.

@KarstenSchnitter KarstenSchnitter added good-first-issue This issue is easy to implement. It can be taken to get into contributing ot the library. log4j This issue is concerned with the log4j integration. labels Jan 21, 2022
KarstenSchnitter added a commit that referenced this issue Jan 23, 2022
Log4j can provide different context data depending on its configuration.
In one case the provided context map is unmodifiable. In that case an
exception occurred, when a custom field was used as a message parameter.
This change guards against that issue, by defensively creating a modifiable
map of properties. Then the custom fields can be added safely.
@KarstenSchnitter KarstenSchnitter linked a pull request Jan 23, 2022 that will close this issue
KarstenSchnitter added a commit that referenced this issue Jan 24, 2022
Log4j can provide different context data depending on its configuration.
In one case the provided context map is unmodifiable. In that case an
exception occurred, when a custom field was used as a message parameter.
This change guards against that issue, by defensively creating a modifiable
map of properties. Then the custom fields can be added safely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good-first-issue This issue is easy to implement. It can be taken to get into contributing ot the library. log4j This issue is concerned with the log4j integration.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant