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

Add errorprone to prevent Log4j direct usage #3759

Merged
merged 9 commits into from
May 3, 2022

Conversation

diega
Copy link
Contributor

@diega diega commented Apr 25, 2022

PR description

This pull request contains some leftovers from #3285 and, most importantly, adds an error prone check for preventing Log4j usage.
Most of this commits were part of #3425 but they mostly abstract stuff from Log4j and are not Logback specific so I thought it would make sense to introduce them here as well.

Here is the full list of changes:

  • HEAD~6: replace Log4j2 logger with Slf4j added since Introduce SLF4J for logging #3285
  • HEAD~5: renames leftover method from Introduce SLF4J for logging #3285
  • HEAD~4: removes Log4j2 usage dependencies relying on the Log4j2ConfiguratorUtil (this is for simplifying the last diff)
  • HEAD~3: replace Log4j2 ThreadContext usage with Slf4j's MDC
  • HEAD~2: this is to explicitly check allowed levels instead of depending on Log4j enum options
  • HEAD~1: for the same reason than before, the level values are filtered by the received string
  • HEAD: adds error prone check for preventing o.a.l.log4j.LogManager usages

Documentation

  • I thought about documentation and added the doc-change-required label to this PR if
    updates are required.

Changelog

diega added 6 commits April 25, 2022 12:09
Signed-off-by: Diego López León <dieguitoll@gmail.com>
Signed-off-by: Diego López León <dieguitoll@gmail.com>
Signed-off-by: Diego López León <dieguitoll@gmail.com>
Signed-off-by: Diego López León <dieguitoll@gmail.com>
Signed-off-by: Diego López León <dieguitoll@gmail.com>
Signed-off-by: Diego López León <dieguitoll@gmail.com>
@@ -25,6 +25,7 @@

import java.util.Arrays;
import java.util.Optional;
import java.util.Set;

import org.apache.logging.log4j.Level;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use of Log4J be removed? How needed is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We can make o.h.b.util.Log4j2ConfiguratorUtil#setAllLevels to receive a string instead of the o.a.l.log4j.Level. That's a good point, it would encapsulate Log4j2 even further

"Do not use junit assertions. Use assertj assertions instead.");
"Do not use junit assertions. Use assertj assertions instead.",
staticMethod().onClass("org.apache.logging.log4j.LogManager"),
"Do not use org.apache.logging.log4j.LogManager, use org.slf4j.LoggerFactory methods instead.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this include the other log4j methods that were removed in this PR? static methods on org.apache.logging.log4j.core.config.Configurator, org.apache.logging.log4j.ThreadContext, instance methods on import org.apache.logging.log4j.core.LoggerContext, org.apache.logging.log4j.core.config.Configuration,
org.apache.logging.log4j.core.config.LoggerConfig? Or is there a way to go full Ender-Wiggin on it and have it fail for any class from the log4j packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we cannot rip off the whole package because we still have hard dependencies on it. The most visible one is o.h.b.util.Log4j2ConfiguratorUtil but we also depend on o.a.l.log4j.Level for the CLI. I went on banning just o.a.l.log4j.LogManager b/c it's the most common entry point to the Log4j framework from the day to day developer perspective

Signed-off-by: Diego López León <dieguitoll@gmail.com>
@macfarla macfarla enabled auto-merge (squash) May 3, 2022 05:20
@sonarqubecloud
Copy link

sonarqubecloud bot commented May 3, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

91.7% 91.7% Coverage
0.0% 0.0% Duplication

@macfarla macfarla merged commit a154034 into hyperledger:main May 3, 2022
@diega diega deleted the logging_tweaks branch May 3, 2022 19:41
@diega diega added the mainnet label May 29, 2022
eum602 pushed a commit to lacchain/besu that referenced this pull request Nov 3, 2023
* Replace Log4j2 API with SLF4j API

* Replace explicit Log4J2 with util call

* Replace ThreadContext with Slf4J's MDC in test

* Inspect raw request parameter for admin_changeLogLevel

* Add errorprone rule to prevent the creation Log4j2 loggers

Signed-off-by: Diego López León <dieguitoll@gmail.com>
Co-authored-by: Sally MacFarlane <sally.macfarlane@consensys.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants