question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replacing log4j with reload4j can cause NoSuchFieldError

See original GitHub issue

This appears to be an interaction between certain versions of the slf4j-log4j12 library and reload4j. If reload4j is used instead of log4j, then using MDC will cause the following stack trace:

Exception in thread "main" java.lang.NoSuchFieldError: tlm
	at org.apache.log4j.MDCFriend.fixForJava9(MDCFriend.java:11)
	at org.slf4j.impl.Log4jMDCAdapter.<clinit>(Log4jMDCAdapter.java:38)
	at Main.main(Main.java:5)

This was discovered by trying to replace log4j with reload4j in the apache Kafka repository, https://github.com/apache/kafka/blob/6eed7743ff6c0e73d65c09bac2e2ad9586cc56ce/gradle/dependencies.gradle#L174

It only occurs in Java versions above 8. The minimal reproduction can be found here: https://github.com/scott-kirk/reload4j-exception I know it’s slf4j code that’s directly causing this, and it is fixed by upgrading slf4j to the latest version, I was just caught off guard by this issue and think it’d be useful to either explicitly say to use the latest version of slf4j or to somehow apply a workaround to avoid needing to also upgrade slf4j.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cekicommented, Feb 18, 2022

SLF4J documentation updated to explain the problem.

See https://www.slf4j.org/codes.html#no_tlm

0reactions
OlivierJaquemetcommented, Jun 3, 2022

@ceki As you indicated in your last message, this backward compatibility problem was fixed in commits https://github.com/qos-ch/reload4j/commit/0cbed4256a40a9e30e2157997cd137fae50f4c57 and https://github.com/qos-ch/reload4j/commit/dc3cc1f245c0cab397ccdf0bf2a416b53b9cee98 . Version 1.2.21 including those commit has been released.

Therefore, I think this issue can be closed. Correct ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Logging not working after replacing the slf4j binding ...
Search for "java.lang.NoSuchFieldError". As per suggestion, I replaced the slf4j binding I was using which is slf4j-log4j12 with slf4j-reload4j.
Read more >
SLF4J Error Codes
Exception in thread "main" java.​​ The NoSuchFieldError is thrown when slf4j-log4j12 attempts to access the 'tlm' package private field in org. apache. log4j....
Read more >
reload4j
By drop-in, we mean the replacement of log4j.jar with reload4j.jar in your build with no source code changes in .java files being necessary....
Read more >
Application log4j initialization fails with 'NoSuchFieldError
Caused by: java.lang.NoSuchFieldError: EMPTY_BYTE_ARRAY at org.apache.logging.log4j.core.config.ConfigurationSource.
Read more >
ITP: reload4j -- Drop-in replacement for Apache log4j 1.2 Java ...
replacement of log4j.jar with reload4j.jar in your build without needing to make changes to source code, i.e., to your java files.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found