[
https://issues.apache.org/jira/browse/LOG4J2-2939?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Constantin Hirsch updated LOG4J2-2939:
--------------------------------------
Description:
When using the SLF4J adapter and you call ThreadContext.containsKey() before
putting anything into the ThreadContext, a NullPointerException is thrown:
{noformat}
Exception in thread "main" java.lang.NullPointerException
at org.apache.logging.slf4j.MDCContextMap.containsKey(MDCContextMap.java:74)
at org.apache.logging.log4j.ThreadContext.containsKey(ThreadContext.java:350)
at
de.semvox.logtest.LogtestApplication.main(LogtestApplication.java:15){noformat}
This is not surprising, as the implementation looks like this
{code:java}
@Override
public boolean containsKey(final String key) {
return MDC.getCopyOfContextMap().containsKey(key);
}
{code}
And the Javadoc for getCopyOfContextMap explicitly states “May be null.”
This bug is also present in the isEmpty method of the same class.
Workaround: Before any tests are made, e.g. top of main, execute the following
{code:java}
ThreadContext.put("foo", "bar");
ThreadContext.remove("foo");{code}
I will create a pull request shortly.
UPDATE: Here is the pull request:
[https://github.com/apache/logging-log4j2/pull/430]
was:
When using the SLF4J adapter and you call ThreadContext.containsKey() before
putting anything into the ThreadContext, a NullPointerException is thrown:
{noformat}
Exception in thread "main" java.lang.NullPointerException
at org.apache.logging.slf4j.MDCContextMap.containsKey(MDCContextMap.java:74)
at
org.apache.logging.log4j.ThreadContext.containsKey(ThreadContext.java:350)
at
de.semvox.logtest.LogtestApplication.main(LogtestApplication.java:15){noformat}
This is not surprising, as the implementation looks like this
{code:java}
@Override
public boolean containsKey(final String key) {
return MDC.getCopyOfContextMap().containsKey(key);
}
{code}
And the Javadoc for getCopyOfContextMap explicitly states “May be null.”
This bug is also present in the isEmpty method of the same class.
Workaround: Before any tests are made, e.g. top of main, execute the following
{code:java}
ThreadContext.put("foo", "bar");
ThreadContext.remove("foo");{code}
I will create a pull request shortly.
UPDATE: Here is the pull request:
[https://github.com/apache/logging-log4j2/pull/430]
> NPE in MDCContextMap
> --------------------
>
> Key: LOG4J2-2939
> URL: https://issues.apache.org/jira/browse/LOG4J2-2939
> Project: Log4j 2
> Issue Type: Bug
> Components: SLF4J Bridge
> Affects Versions: 2.13.3
> Environment: Empty fresh Spring Boot project on Windows 10 64Bit
> Oracle JDK11
> Reporter: Constantin Hirsch
> Priority: Major
>
> When using the SLF4J adapter and you call ThreadContext.containsKey() before
> putting anything into the ThreadContext, a NullPointerException is thrown:
> {noformat}
> Exception in thread "main" java.lang.NullPointerException
> at org.apache.logging.slf4j.MDCContextMap.containsKey(MDCContextMap.java:74)
> at org.apache.logging.log4j.ThreadContext.containsKey(ThreadContext.java:350)
> at
> de.semvox.logtest.LogtestApplication.main(LogtestApplication.java:15){noformat}
> This is not surprising, as the implementation looks like this
> {code:java}
> @Override
> public boolean containsKey(final String key) {
> return MDC.getCopyOfContextMap().containsKey(key);
> }
> {code}
> And the Javadoc for getCopyOfContextMap explicitly states “May be null.”
> This bug is also present in the isEmpty method of the same class.
>
> Workaround: Before any tests are made, e.g. top of main, execute the following
> {code:java}
> ThreadContext.put("foo", "bar");
> ThreadContext.remove("foo");{code}
>
> I will create a pull request shortly.
> UPDATE: Here is the pull request:
> [https://github.com/apache/logging-log4j2/pull/430]
--
This message was sent by Atlassian Jira
(v8.3.4#803005)