Skip to content

Add recipe to change logger fields to private #221

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

Merged

Conversation

jhl221123
Copy link
Contributor

@jhl221123 jhl221123 commented May 30, 2025

What's changed?

This PR introduces ChangeLoggersToPrivate, a new recipe enforcing private visibility for logger fields.

What's your motivation?

Addresses issue #219, promoting better encapsulation of logging mechanics.

Anything in particular you'd like reviewers to focus on?

Correctness of modifier changes across different logger types and existing modifiers.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite May 30, 2025
@timtebeek timtebeek added the recipe Recipe Requested label May 30, 2025
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite May 30, 2025
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the help here @jhl221123 ! I've added some light polishing commits. Preconditions should help keep this recipe performant, by only traversiong through compilation units that use a logger at all. The ListUtils is a slightly more expressive way of manipulating elements. And the prefix wrangling that was there previously is no longer necessary. I've also added your recipe to be included with our SLF4J best practices, such that folks do no have to specifically seek out this recipe to get the benefits, but can run the broader best practices.

We acknowledge that if folks were to use the logger field from another class that might now break; we can optionally follow up with a change to make this a scanning recipe that detects such usage, and in those cases does not make any changes. I'd consider that rare though, and tolerable for a best practices recipe.

timtebeek and others added 2 commits May 30, 2025 14:51
…ate.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@timtebeek timtebeek merged commit 14d9d55 into openrewrite:main May 30, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite May 30, 2025
@jhl221123
Copy link
Contributor Author

@timtebeek, Thanks for the polishing commits and for merging this! I've learned a lot more about OpenRewrite thanks to your guidance during this process. I'll be sure to keep these points in mind for even better contributions next time. Thank you!

@timtebeek
Copy link
Member

Thanks a lot! Looking forward to what you'll do next. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe Requested
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Loggers should be always private
2 participants